//
//  init.m
//  Estate
//
//  Created by heyong on 16/7/29.
//  Copyright © 2016年 ttsx. All rights reserved.
//

#import "dataInit.h"
#import "SampleProcess.h"
#import "SampleProcessRecord.h"
#import "InspectProcess.h"
#import "InspectProcessRecord.h"
#import "InspectItem.h"
#import "InspectUserItem.h"
#import "InspectGroup.h"
#import "InspectGroupUser.h"
#import "InspectTotalRecord.h"
#import "SampleCode.h"
#import "FMDatabaseAdditions.h"
#import "PatrolRecord.h"
#import "SecurityRecordBasic.h"
#import "SecurityPatrol.h"
#import "ThreeTypePeople.h"
#import "DangerousWork.h"
#import "SpecialOperationsPersonnel.h"
#import "WorkDeviceApproach.h"

@implementation dataInit

- (FMDatabase *)db{
    if(_db==nil){
        _db=[FMDatabase databaseWithPath:DEF_DB_DATABASEPATH];
    }
    return _db;
}
- (UILabel *)lab{
    if(_lab==nil){
        _lab=[[UILabel alloc]init];
    }
    return _lab;
}
- (NSMutableDictionary *)fileDic{
    if(_fileDic==nil)
    {
        _fileDic=[NSMutableDictionary dictionary];
    }
    return _fileDic;
}

- (UIProgressView *)progress{
    if(_progress==nil){
        _progress=[[UIProgressView alloc]init];
    }
    return _progress;
}

-(void)initWithResponseObject:(id)object{
    [self setMBProgressHudView:@"数据正在初始化...."];
    [self.hud show:YES];
    [self createFile];
    
    // 存储地址表
    self.addressArray=object[@"addressList"];
    if(self.addressArray.count>0){
        [self saveAddress:self.addressArray];
    }
    
    // 存储单位表
    self.unitArray=object[@"unitList"];
    if(self.unitArray.count>0){
        [self saveUnit:self.unitArray];
    }
    
    // 存储人员表
    self.userArray=object[@"userList"];
    if(self.userArray.count>0){
        [self saveUser:self.userArray];
    }
    
    // 存储项目单位表
    self.proUnitsArray=object[@"proUnitsList"];
    if(self.proUnitsArray.count>0){
        [self saveProUnits:self.proUnitsArray];
    }
    
    // 存储流程表
    self.processArray=object[@"processList"];
    if(self.processArray.count>0){
        [self saveProcess:self.processArray];
    }
    
    // 流程记录
    self.processRecordArray=object[@"processRecordList"];
    if(self.processRecordArray.count>0){
        [self saveProcessRecord:self.processRecordArray];
    }
    
    // 项目表
    self.projectArray=object[@"projectList"];
    if(self.projectArray.count>0){
        [self saveProject:self.projectArray];
    }
    
    // 存储单位人员
    self.proUsersArray=object[@"proUsersList"];
    if(self.proUsersArray.count>0){
        [self saveProUsers:self.proUsersArray];
    }
    // 存储项目工程概况
    self.proEngineerSurveyArray=object[@"proEngineerSurveyList"];
    if(self.proEngineerSurveyArray.count>0){
        [self saveProEngineerSurvey:self.proEngineerSurveyArray];
    }
    
    // 存储日记表
    self.workDiaryArray=object[@"workDiaryList"];
    if(self.workDiaryArray.count>0){
        [self saveDiary:self.workDiaryArray];
    }
    
    // 存储日志表
    self.workLogArray=object[@"workLogList"];
    if(self.workLogArray.count>0){
        [self saveWorkLog:self.workLogArray];
    }
    
    // 存储记录表
    self.workRecordArray=object[@"workRecordList"];
    if(self.workRecordArray.count>0){
        [self saveWorkRecord:self.workRecordArray];
    }
    // 广告
    self.advertisementArray=object[@"advertisingPicture"];
    if(self.advertisementArray.count >0){
        [self saveAdvertisement:self.advertisementArray];
    }
    
    // 配置表
    self.systemConfigArray=object[@"systemConfig"];
    if(self.systemConfigArray.count >0){
        [self saveSystemConfigArray:self.systemConfigArray];
    }
    
    // 见证取样流程
    self.sampleProcessArray = object[@"sampleList"];
    if(self.sampleProcessArray.count > 0) {
        [self saveSampleProcess:self.sampleProcessArray];
    }

    // 见证取样记录
    self.samplePRArray = object[@"sampleProcessRecordList"];
    if(self.samplePRArray.count > 0) {
        [self saveSampleRecord:self.samplePRArray];
    }
    
    // 见证取样二维码表
    self.sampleCodeArray = object[@"sampleCodeList"];
    if (self.sampleCodeArray.count > 0) {
        [self saveSampleCode:self.sampleCodeArray];
    }
  
    // 巡检流程
    self.inspectProcessArray = object[@"inspectProcessList"];
    if(self.inspectProcessArray.count > 0) {
        [self saveInspectProcess:self.inspectProcessArray];
    }
    
    // 巡检流程记录
    self.inspectPRArray = object[@"inspectProcessRecordList"];
    if(self.inspectPRArray.count > 0) {
        [self saveInspectProcessRecord:self.inspectPRArray];
    }
    
    // 巡检项
    self.inspectItemArray = object[@"inspectItemList"];
    if(self.inspectItemArray.count > 0) {
        [self saveInspectItem:self.inspectItemArray];
    }
    
    // 用户巡检项
    self.inspectUserItemArray = object[@"inspectUserItemList"];
    if(self.inspectUserItemArray.count > 0) {
        [self saveInspectUserItem:self.inspectUserItemArray];
    }
    
    // 巡检组
    self.inspectGroupArray = object[@"inspectGroupList"];
    if(self.inspectGroupArray.count > 0) {
        [self saveInspectGroup:self.inspectGroupArray];
    }
    
    // 用户巡检组
    self.inspectGroupUserArray = object[@"inspectGroupUserList"];
    if(self.inspectGroupUserArray.count > 0) {
        [self saveInspectGroupUser:self.inspectGroupUserArray];
    }
    
    // 一键汇总
    self.inspectTotalRecordArray = object[@"inspectTotalRecordList"];
    if(self.inspectTotalRecordArray.count > 0) {
        [self saveInspectTotalRecord:self.inspectTotalRecordArray];
    }
    
    // 巡视记录
    self.patrolRecordArray = object[@"patrolRecordList"];
    if(self.patrolRecordArray.count > 0) {
        [self savePatrolRecordArray:self.patrolRecordArray];
    }
    
    // 安全记录基本表
    self.securityRecordBasicArray = object[@"safeRecordBasicList"];
    if(self.securityRecordBasicArray.count > 0) {
        [self saveSecurityRecordBasic:self.securityRecordBasicArray];
    }
    
    // 安全记录-安全巡查
    self.securityPatrolArray = object[@"securityPatrolList"];
    if(self.securityPatrolArray.count > 0) {
        [self saveSecurityPatrol:self.securityPatrolArray];
    }
    
    // 安全记录-施工三类人员
    self.threeTypePeopleArray = object[@"safeConstructionList"];
    if(self.threeTypePeopleArray.count > 0) {
        [self saveThreeTypePeople:self.threeTypePeopleArray];
    }
    
    // 安全记录-危险性较大工程
    self.dangerousWorkArray = object[@"safetyRiskList"];
    if(self.dangerousWorkArray.count > 0) {
        [self saveDangerousWork:self.dangerousWorkArray];
    }
    
    // 安全记录-特种作业人员
    self.specialOperationsPersonnelArray = object[@"safeSpecialTypeList"];
    if(self.specialOperationsPersonnelArray.count > 0) {
        [self saveSpecialOperationsPersonnel:self.specialOperationsPersonnelArray];
    }
    
    // 安全记录-施工设备进场
    self.workDeviceApproachArray = object[@"safeConstructDeviceList"];
    if(self.workDeviceApproachArray.count > 0) {
        [self saveWorkDeviceApproach:self.workDeviceApproachArray];
    }
    
    // 附件
    self.attachmentArray = object[@"attachmentList"];
    if(self.attachmentArray.count > 0) {
        [self saveAttachment:self.attachmentArray andDownLoad:NO];
    }
    
    _downThread = [[NSThread alloc]initWithTarget:self selector:@selector(downloadFile) object:nil];
    [_downThread start];
}

//配置表
-(void)saveSystemConfigArray:(NSMutableArray *)systemConfigArray{
    
    NSMutableArray *modelArr=[NSMutableArray array];
    for (int i=0; i<systemConfigArray.count; i++) {
        SystemConfig *addressM=[SystemConfig yy_modelWithJSON:systemConfigArray[i]];
        [modelArr addObject:addressM];
    }
    
    [self.db open];
    [self.db setShouldCacheStatements:YES];
    
    static NSString *insertSQLStatment = @"INSERT OR REPLACE INTO SYSTEM_CONFIG ('ID','CONFNAME','CONFVALUE','CONFDESC','SORT','CREATE_USER_NAME','CREATE_TIME','CREATE_IP','MODIFY_USER_NAME','MODIFY_TIME','MODIFY_IP','LOCAL_MODIFY_STATE') VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    
    [self.db beginTransaction];
    
    for (SystemConfig *ad in modelArr)
    {
        [self.db executeUpdate:insertSQLStatment withArgumentsInArray:@[ad.id,DEFISNUL(ad.confname),DEFISNUL(ad.confvalue),DEFISNUL(ad.confdesc),DEFISNUL(ad.sort),DEFISNUL(ad.createusername),DEFISNUL(ad.createtime),DEFISNUL(ad.createip),DEFISNUL(ad.modifyusername),DEFISNUL(ad.modifytime),DEFISNUL(ad.modifyip),@""]];
    }
    [self.db commit];
    [self.db close];
}

// 位置信息保存
- (void)saveAddress:(NSMutableArray *)addressArray{
    
    NSMutableArray *modelArr=[NSMutableArray array];
    for (int i=0; i<addressArray.count; i++) {
        Address *addressM=[Address yy_modelWithJSON:addressArray[i]];
        [modelArr addObject:addressM];
    }
    
    [self.db open];
    [self.db setShouldCacheStatements:YES];
    
    static NSString *insertSQLStatment = @"INSERT OR REPLACE INTO ADDRESS ('ID', 'PRO_ID', 'PATENT_ID', 'ADDRESS_NAME', 'ADDRESS_TYPE', 'A__LONG', 'A__LAT', 'A__HEIGHT', 'B__LONG', 'B__LAT', 'B__HEIGHT', 'C__LONG', 'C__LAT', 'C__HEIGHT', 'D__LONG', 'D__LAT', 'D__HEIGHT', 'STATE', 'IMAGE_INFO', 'CREATE_USER_NAME', 'CREATE_TIME', 'CREATE_IP', 'MODIFY_USER_NAME', 'MODIFY_IP', 'MODIFY_TIME', 'LOCAL_MODIFY_USER_NAME', 'LOCAL_MODIFY_TIME', 'LOCAL_MODIFY_STATE', 'GPS_A_LAT', 'GPS_A_LONG', 'GPS_C_LAT', 'GPS_C_LONG', 'LEFT_NAME', 'RIGHT_NAME', 'UP_NAME', 'DOWN_NAME', 'DISTANCE_OF_LEFTANDRIGHT', 'DISTANCE_OF_UPANDDOWN') VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    
    [self.db beginTransaction];
    for (Address *ad in modelArr)
    {
        [self.db executeUpdate:insertSQLStatment withArgumentsInArray:@[ad.id, ad.proId, ad.parentId, ad.addName, DEFISNUL(ad.addType), DEFISNUL(ad.along), DEFISNUL(ad.alat), DEFISNUL(ad.aheight), DEFISNUL(ad.blong), DEFISNUL(ad.blat), DEFISNUL(ad.bheight), DEFISNUL(ad.clong), DEFISNUL(ad.clat), DEFISNUL(ad.cheight), DEFISNUL(ad.dlong), DEFISNUL(ad.dlat), DEFISNUL(ad.dheight), DEFISNUL(ad.state), DEFISNUL(ad.imageInfo), DEFISNUL(ad.createusername), DEFISNUL(ad.createtime), DEFISNUL(ad.createip), DEFISNUL(ad.modifyusername), DEFISNUL(ad.modifyip), DEFISNUL(ad.modifytime), @"", @"", @"", DEFISNUL(ad.GPSaLat), DEFISNUL(ad.GPSaLong), DEFISNUL(ad.GPScLat), DEFISNUL(ad.GPScLong), DEFISNUL(ad.leftName), DEFISNUL(ad.rightName), DEFISNUL(ad.upName), DEFISNUL(ad.downName), DEFISNUL(ad.distanceOfLeftandright), DEFISNUL(ad.distanceOfUpanddown)]];
    }
    [self.db commit];
    [self.db close];
}

// 存储单位表
- (void)saveUnit:(NSMutableArray *)unitArray{
    NSMutableArray *modelArr=[NSMutableArray array];
    for (int i=0; i<unitArray.count; i++) {
        Unit *addressM=[Unit yy_modelWithJSON:unitArray[i]];
        [modelArr addObject:addressM];
    }
    
    [self.db open];
    [self.db setShouldCacheStatements:YES];
    
    static NSString *insertSQLStatment = @"INSERT OR REPLACE INTO UNIT ('ID','ORGNAME','ORGENGLISH_NAME','ORGID','PARENT_ID','UNIT_LOGO','UNIT_PHONE','UNIT_ADDRESS','UNIT_ZIP_CODE','UNIT_EMAIL','UNIT_WEB','STATE','IS_STOP','SORT','CREATE_USER_NAME','CREATE_TIME','CREATE_IP','MODIFY_USER_NAME','MODIFY_IP','MODIFY_TIME','LOCAL_MODIFY_USER_NAME','LOCAL_MODIFY_TIME','LOCAL_MODIFY_STATE','IS_SUPERVISION_UNIT') VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?,?,?,?)";
    
    [self.db beginTransaction];
    
    for (Unit *un in modelArr)
    {
        [self.db executeUpdate:insertSQLStatment withArgumentsInArray:@[un.id,un.unitname,DEFISNUL(un.unitenglishname) ,DEFISNUL(un.unitid),DEFISNUL(un.parentid),DEFISNUL(un.unitenglishname),DEFISNUL(un.unitphone),DEFISNUL(un.unitaddress),DEFISNUL(un.zipcode),DEFISNUL(un.unitemail),DEFISNUL(un.unitweb),DEFISNUL(un.state),DEFISNUL(un.istop),DEFISNUL(un.sort),DEFISNUL(un.createusername),DEFISNUL(un.createtime),DEFISNUL(un.createip),DEFISNUL(un.modifyusername),DEFISNUL(un.modifyip),DEFISNUL(un.modifytime),@"",@"",@"",DEFISNUL(un.isSuperVisionUnit)]];
    }
    
    [self.db commit];
    [self.db close];
}

// 存储人员表
- (void)saveUser:(NSMutableArray *)userArray{
    NSMutableArray *modelArr=[NSMutableArray array];
    for (int i=0; i<userArray.count; i++) {
        User *addressM=[User yy_modelWithJSON:userArray[i]];
        [modelArr addObject:addressM];
    }
    
    [self.db open];
    [self.db setShouldCacheStatements:YES];
    
    static NSString *insertSQLStatment = @"INSERT OR REPLACE INTO USER ('ID','USER_NAME','REAL_NAME','PASSWORD','STATE','UNIT','EMAIL','MOBILE_PHONE','HEAD_PHOTO','SEX','BIRTHDAY','EDU','ADDRESS','JOB_NUMBER','JOB','WORK_YEAR','POLITICS_STATUS','HOBBY','LAST_LOGIN_TIME','LAST_LOGIN_IP','LOGIN_CNT','IS_DELETE','SORT','TITLE_TYPE','TITLE_NAME','EXPIRE_TIME','IS_CHECK','CREATE_USER_NAME','CREATE_TIME','CREATE_IP','MODIFY_USER_NAME','MODIFY_IP','MODIFY_TIME','LOCAL_MODIFY_USER_NAME','LOCAL_MODIFY_TIME','LOCAL_MODIFY_STATE', 'EXTEND_INFO') VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    
    [self.db beginTransaction];
    
    for (User *user in modelArr)
    {
        [self.db executeUpdate:insertSQLStatment withArgumentsInArray:@[user.id,user.username,DEFISNUL(user.realname),DEFISNUL(user.password),DEFISNUL(user.state),DEFISNUL(user.unit),DEFISNUL(user.email),DEFISNUL(user.mobilephone),DEFISNUL(user.headPath),DEFISNUL(user.sex),@"",DEFISNUL(user.edu),DEFISNUL(user.address),DEFISNUL(user.jobnumber),DEFISNUL(user.job),DEFISNUL(user.workyear),DEFISNUL(user.politicsstatus),DEFISNUL(user.hobby),DEFISNUL(user.lastlogintime),DEFISNUL(user.lastloginip),DEFISNUL(user.loginCnt),DEFISNUL(user.isdelete),DEFISNUL(user.sort), DEFISNUL(user.titleType),DEFISNUL(user.titleName),DEFISNUL(user.expireTime),DEFISNUL(user.isCheck),       DEFISNUL(user.createusername),DEFISNUL(user.createtime),DEFISNUL(user.createip),DEFISNUL(user.modifyusername),DEFISNUL(user.modifyip),DEFISNUL(user.modifytime),@"",@"",@"", DEFISNUL(user.extendInfo)]];
    }
    [self.db commit];
    [self.db close];
}

// 存储项目单位表
- (void)saveProUnits:(NSMutableArray *)proUnitsArray {
    NSMutableArray *modelArr=[NSMutableArray array];
    for (int i=0; i<proUnitsArray.count; i++) {
        ProjectUnit *addressM=[ProjectUnit yy_modelWithJSON:proUnitsArray[i]];
        [modelArr addObject:addressM];
    }
    
    [self.db open];
    [self.db setShouldCacheStatements:YES];
    
    static NSString *insertSQLStatment = @"INSERT OR REPLACE INTO PROJECT_UNIT ('ID','PRO_ID','PRO_UNIT_TYPE','UNIT_ID','PRO_UNIT_NAME','PRO_UNIT_PHONE','PRO_UNIT_ADDRESS','PRO_UNIT_EMAIL','PRO_UNIT_DESC','CONTRCT_NUMBER','CONTRCT_MONEY','CREATE_USER_NAME','CREATE_TIME','CREATE_IP','MODIFY_USER_NAME','MODIFY_IP','MODIFY_TIME','LOCAL_MODIFY_USER_NAME','LOCAL_MODIFY_TIME','LOCAL_MODIFY_STATE', 'EXTEND_INFO') VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    
    [self.db beginTransaction];
    
    for (ProjectUnit *pro in modelArr)
    {
        [self.db executeUpdate:insertSQLStatment withArgumentsInArray:@[pro.id,pro.proId,DEFISNUL(pro.proUnitType),DEFISNUL(pro.unitId),DEFISNUL(pro.proUnitname),DEFISNUL(pro.proUnitPhone),DEFISNUL(pro.proUnitAddress),DEFISNUL(pro.proUnitEmail),DEFISNUL(pro.proUnitDesc),DEFISNUL(pro.contrctNumber),DEFISNUL(pro.contrctMoney),DEFISNUL(pro.createusername),DEFISNUL(pro.createtime),DEFISNUL(pro.createip),DEFISNUL(pro.modifyssername),DEFISNUL(pro.modifyip),DEFISNUL(pro.modifytime), @"", @"", @"", DEFISNUL(pro.extendInfo)]];
    }
    [self.db commit];
    [self.db close];
}

// 存储流程表
- (void)saveProcess:(NSMutableArray *)processArray{
    NSMutableArray *modelArr=[NSMutableArray array];
    for (int i=0; i<processArray.count; i++) {
        Process *addressM=[Process yy_modelWithJSON:processArray[i]];
        [modelArr addObject:addressM];
    }
    
    [self.db open];
    [self.db setShouldCacheStatements:YES];
    
    static NSString *insertSQLStatment = @"INSERT OR REPLACE INTO Process ('ID','PRO_ID','ADDRESS_ID','ADDRES_INFO','PROCESS_TYPE','PROCESS_STAGE','PROCESS_PUBLISHERS','PROCESS_MASTERS','PROCESS_RECIFY_USERS','PROCESS_RECIFY_UNITS','RECIFY_REVIEW_USERS','BEGIN_TIME','REQUIRED_END_TIME','END_TIME','PROCESS_TITLE','PROCESS_NUMBER','EXTEND_INFO','CREATE_USER_NAME','CREATE_TIME','CREATE_IP','MODIFY_USER_NAME','MODIFY_IP','MODIFY_TIME','LOCAL_MODIFY_USER_NAME','LOCAL_MODIFY_TIME','LOCAL_MODIFY_STATE') VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    
    [self.db beginTransaction];
    
    for (Process *pro in modelArr)
    {
        [self.db executeUpdate:insertSQLStatment withArgumentsInArray:@[pro.id,pro.proId,DEFISNUL(pro.addressId),DEFISNUL(pro.addressInfo),DEFISNUL(pro.processType),DEFISNUL(pro.processStage),DEFISNUL(pro.processPublishers),DEFISNUL(pro.processMasters),DEFISNUL(pro.processRecifyUsers),DEFISNUL(pro.processRecifyUnits),DEFISNUL(pro.recifyReviewUsers),DEFISNUL(pro.beginTime),DEFISNUL(pro.requiredEndtime),DEFISNUL(pro.endTime),DEFISNUL(pro.processTitle),DEFISNUL(pro.processNumber),DEFISNUL(pro.extendInfo),DEFISNUL(pro.createusername),DEFISNUL(pro.createtime),DEFISNUL(pro.createip),DEFISNUL(pro.modifyusername),DEFISNUL(pro.modifyip),DEFISNUL(pro.modifytime),@"",@"",@""]];
    }
    [self.db commit];
    [self.db close];
}

// 流程记录
- (void)saveProcessRecord:(NSMutableArray *)processRecordArray{
    
    NSMutableArray *modelArr=[NSMutableArray array];
    for (int i=0; i<processRecordArray.count; i++) {
        ProcessRecord *addressM=[ProcessRecord yy_modelWithJSON:processRecordArray[i]];
        [modelArr addObject:addressM];
    }
    
    [self.db open];
    [self.db setShouldCacheStatements:YES];
    
    static NSString *insertSQLStatment = @"INSERT OR REPLACE INTO PROCESS_RECORD ('ID','PRO_ID','PROCESS_ID','WEATHER','ADDRESS_ID','ADDRES_INFO','PROCESS_TYPE','PROCESS_STAGE','RECORD_USER','PROBLEM_TYPE','RECORD_MASTER','RECIFY_USER','RECIFY_UNIT','SEVERITY','END_TIME','SEND_TIME','SEND_TYPE','PROCESS_TITLE','PROCESS_CONTENT','PROCESS_REQUIRED','PROCESS_DESC','SUBMITTORS','PUT_USER','REVIEW_USER','REVIEW_OPINION','REVIEW_STATE','FILEINFOS','EXTEND_INFO','CREATE_USER_NAME','CREATE_TIME','CREATE_IP','MODIFY_USER_NAME','MODIFY_IP','MODIFY_TIME','LOCAL_MODIFY_USER_NAME','LOCAL_MODIFY_TIME','LOCAL_MODIFY_STATE') VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?)";
    
    [self.db beginTransaction];
    
    for (ProcessRecord *pro in modelArr)
    {
        [self.db executeUpdate:insertSQLStatment withArgumentsInArray:@[pro.id,pro.proId,DEFISNUL(pro.processId),DEFISNUL(pro.weather),DEFISNUL(pro.addressId),DEFISNUL(pro.addressInfo),DEFISNUL(pro.processType),DEFISNUL(pro.processStage),DEFISNUL(pro.recordUser),DEFISNUL(pro.problemType),DEFISNUL(pro.recordMaster),DEFISNUL(pro.recifyUser),DEFISNUL(pro.recifyUnit),DEFISNUL(pro.severity),DEFISNUL(pro.endtime),DEFISNUL(pro.sendtime),DEFISNUL(pro.sendtype),DEFISNUL(pro.processTitle),DEFISNUL(pro.processContent),DEFISNUL(pro.processRequired),DEFISNUL(pro.processDesc),DEFISNUL(pro.submittors),DEFISNUL(pro.putUser),DEFISNUL(pro.reviewUser),DEFISNUL(pro.reviewOpinion),DEFISNUL(pro.reviewState),DEFISNUL(pro.fileinfos),DEFISNUL(pro.extendInfo),DEFISNUL(pro.createusername),DEFISNUL(pro.createtime),DEFISNUL(pro.createip),DEFISNUL(pro.modifyusername),DEFISNUL(pro.modifyip),DEFISNUL(pro.modifytime),@"",@"",@""]];
    }
    
    [self.db commit];
    
    [self.db close];
    
}


- (void)saveProject:(NSMutableArray *)projectArray{
    
    NSMutableArray *modelArr=[NSMutableArray array];
    for (int i=0; i<projectArray.count; i++) {
        Project *addressM=[Project yy_modelWithJSON:projectArray[i]];
        [modelArr addObject:addressM];
    }
    
    [self.db open];
    [self.db setShouldCacheStatements:YES];
    
    static NSString *insertSQLStatment = @"INSERT OR REPLACE INTO Project ('ID','PROJECT_NAME','PROJECT_TYPE','PROJECT_NUMBER','PROJECT_STATE','STATE','CREATE_USER_NAME','CREATE_TIME','CREATE_IP','MODIFY_USER_NAME','MODIFY_IP','MODIFY_TIME','LOCAL_MODIFY_USER_NAME','LOCAL_MODIFY_TIME','LOCAL_MODIFY_STATE','EXTEND_INFO') VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    
    [self.db beginTransaction];
    
    for (Project *pro in modelArr)
    {
        [self.db executeUpdate:insertSQLStatment withArgumentsInArray:@[pro.id,pro.proName,DEFISNUL(pro.proType),DEFISNUL(pro.proNumber),DEFISNUL(pro.proState),DEFISNUL(pro.state),DEFISNUL(pro.createusername),DEFISNUL(pro.createtime),DEFISNUL(pro.createip),DEFISNUL(pro.modifyusername),DEFISNUL(pro.modifyip),DEFISNUL(pro.modifytime),@"",@"",@"",DEFISNUL(pro.extendInfo)]];
    }
    
    [self.db commit];
    [self.db close];
    
}

// 存储单位人员
- (void)saveProUsers:(NSMutableArray *)proUsersArray{
    //    NSLog(@"proUsersArray:%@",proUsersArray);
    NSMutableArray *modelArr=[NSMutableArray array];
    for (int i=0; i<proUsersArray.count; i++) {
        ProjectUser *addressM=[ProjectUser yy_modelWithJSON:proUsersArray[i]];
        [modelArr addObject:addressM];
    }
    
    [self.db open];
    [self.db setShouldCacheStatements:YES];
    
    static NSString *insertSQLStatment = @"INSERT OR REPLACE INTO PROJECT_USER ('ID','PRO_ID','UNIT_ID','USER_ID','USER_TYPE','PRO_USER_DESC','TITLE_TYPE','TITLE_NAME','EXPIRE_TIME','IS_CHECK','CREATE_USER_NAME','CREATE_TIME','CREATE_IP','MODIFY_USER_NAME','MODIFY_IP','MODIFY_TIME','LOCAL_MODIFY_USER_NAME','LOCAL_MODIFY_TIME','LOCAL_MODIFY_STATE','EXTEND_INFO') VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    
    [self.db beginTransaction];
    
    for (ProjectUser *pro in modelArr)
    {
        [self.db executeUpdate:insertSQLStatment withArgumentsInArray:@[pro.id,pro.proId,DEFISNUL(pro.unitid),DEFISNUL(pro.userid),DEFISNUL(pro.userType),DEFISNUL(pro.proUserDesc),DEFISNUL(pro.titleType),DEFISNUL(pro.titleName),DEFISNUL(pro.expireTime),DEFISNUL(pro.isCheck), DEFISNUL(pro.createusername),DEFISNUL(pro.createtime),DEFISNUL(pro.createip),DEFISNUL(pro.modifyusername),DEFISNUL(pro.modifyip),DEFISNUL(pro.modifytime),@"",@"",@"",DEFISNUL(pro.extendInfo)]];
    }
    [self.db commit];
    [self.db close];
}

// 项目工程概况
- (void)saveProEngineerSurvey:(NSMutableArray *)proEngineerSurveyArray {
    NSMutableArray *modelArr=[NSMutableArray array];
    for (int i=0; i<proEngineerSurveyArray.count; i++) {
        ProEngineerSurvey *addressM=[ProEngineerSurvey yy_modelWithJSON:proEngineerSurveyArray[i]];
        [modelArr addObject:addressM];
    }
    
    [self.db open];
    [self.db setShouldCacheStatements:YES];
    
    static NSString *insertSQLStatment = @"INSERT OR REPLACE INTO PRO_ENGINEER_SURVEY ('ID','PRO_ID','CITY_CODE','ENGINEER_ADDRESS','BUILDING_SCALE','FREE_SERVICE_TIME','GUARANTEE_TIME','SERVICE_TIME','CONSTRACT_AMOUNT','INVESTMENT_AMOUNT','ENGINEER_TYPE','INVESTMENT_NATURE','START_WORK_TIME','COMPLETE_WORK_TIME','PRO_SURVEY','PRO_ENGINEER_DESC','CREATE_USER_NAME','CREATE_TIME','CREATE_IP','MODIFY_USER_NAME','MODIFY_IP','MODIFY_TIME','LOCAL_MODIFY_USER_NAME','LOCAL_MODIFY_TIME','LOCAL_MODIFY_STATE','EXTEND_INFO', 'CENTER_LONG', 'CENTER_LAT', 'CENTER_HEIGHT', 'CITY_ID') VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    
    [self.db beginTransaction];
    //请求下来的是cityId":"110101 ·   "//东城区 获取的天气是110102西城区
    for (ProEngineerSurvey *pro in modelArr)
    {
        [self.db executeUpdate:insertSQLStatment withArgumentsInArray:@[pro.id,pro.proId,DEFISNUL(pro.cityCode),DEFISNUL(pro.engineerAddress),DEFISNUL(pro.buildingScale),DEFISNUL(pro.freeServiceTime),DEFISNUL(pro.guaranteeTime),DEFISNUL(pro.serviceTime),DEFISNUL(pro.constractAmount),DEFISNUL(pro.investmentAmount),DEFISNUL(pro.engineerType),DEFISNUL(pro.investmentNature),DEFISNUL(pro.startworkTime),DEFISNUL(pro.completeworkTime),DEFISNUL(pro.proSurvey),DEFISNUL(pro.proEngineerDesc),DEFISNUL(pro.createusername),DEFISNUL(pro.createtime),DEFISNUL(pro.createip),DEFISNUL(pro.modifyusername),DEFISNUL(pro.modifyip),DEFISNUL(pro.modifytime),@"",@"",@"",DEFISNUL(pro.extendInfo), DEFISNUL(pro.centerLong), DEFISNUL(pro.centerLat), DEFISNUL(pro.centerHeight), DEFISNUL(pro.cityId)]];
    }
    
    [self.db commit];
    [self.db close];
}

// 存储日记表
- (void)saveDiary:(NSMutableArray *)workDiaryArray{
    NSMutableArray *modelArr=[NSMutableArray array];
    for (int i=0; i<workDiaryArray.count; i++) {
        WorkDiary *addressM=[WorkDiary yy_modelWithJSON:workDiaryArray[i]];
        [modelArr addObject:addressM];
    }
    [self.db open];
    //迭代 记录新增类型字段 判断有没有。没有数据库增加此字段。
    if (![self.db columnExists:@"ZHILIANG_INFO" inTableWithName:WORK_DIARY]){
        [self.db beginTransaction];
        
        NSString *alertStr1 = [NSString stringWithFormat:@"ALTER TABLE WORK_DIARY ADD 'JIANCHA_INFO' TEXT "];
        [self.db executeUpdate:alertStr1];
        
        NSString *alertStr2 = [NSString stringWithFormat:@"ALTER TABLE WORK_DIARY ADD 'ZHILIANG_INFO' TEXT"];
        [self.db executeUpdate:alertStr2];
        
        NSString *alertStr3 = [NSString stringWithFormat:@"ALTER TABLE WORK_DIARY ADD 'JINDU_INFO' TEXT"];
        [self.db executeUpdate:alertStr3];
        
        NSString *alertStr4 = [NSString stringWithFormat:@"ALTER TABLE WORK_DIARY ADD 'CAILIAO_INFO' TEXT"];
        [self.db executeUpdate:alertStr4];
        
        NSString *alertStr5 = [NSString stringWithFormat:@"ALTER TABLE WORK_DIARY ADD 'YINBI_INFO' TEXT"];
        [self.db executeUpdate:alertStr5];
        
        NSString *alertStr6 = [NSString stringWithFormat:@"ALTER TABLE WORK_DIARY ADD 'SHOUFAWEN_INFO' TEXT"];
        [self.db executeUpdate:alertStr6];
        
        NSString *alertStr7 = [NSString stringWithFormat:@"ALTER TABLE WORK_DIARY ADD 'JIANZHENG_INFO' TEXT"];
        [self.db executeUpdate:alertStr7];
        
        NSString *alertStr8 = [NSString stringWithFormat:@"ALTER TABLE WORK_DIARY ADD 'HUIYI_INFO' TEXT"];
        [self.db executeUpdate:alertStr8];
        
        [self.db commit];
    }

    
    [self.db open];
    [self.db setShouldCacheStatements:YES];
    
    static NSString *insertSQLStatment = @"INSERT OR REPLACE INTO WORK_DIARY ('ID','PRO_ID','DIARY_USER','DIARY_TYPE','DIARY_TIME','REVIEW_TIME','TOUR_INFO','CHECK_INFO','SITE_INFO','WEATHER','SUPERVISE_INFO','FOLLOW_INFO','SAFE_INFO','OTHER_INFO','ENTER_EXTER_COMM_INFO','MEETING_INFO','REVIEW_INFO','REVIEW_SCORE','REVIEW_USER','REVIEW_STATE','FILEIDS','DIARY_DESC','EXTEND_INFO','STATE','CREATE_USER_NAME','CREATE_TIME','CREATE_IP','MODIFY_USER_NAME','MODIFY_IP','MODIFY_TIME','LOCAL_MODIFY_USER_NAME','LOCAL_MODIFY_TIME','LOCAL_MODIFY_STATE','JIANCHA_INFO','ZHILIANG_INFO','JINDU_INFO','CAILIAO_INFO','YINBI_INFO','SHOUFAWEN_INFO','JIANZHENG_INFO','HUIYI_INFO', 'PATROL_RECORD_INFO', 'SAFE_RECORD_INFO') VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?,?,?,?,?,?,?,?, ?, ?)";
    
    [self.db beginTransaction];
    
    for (WorkDiary *pro in modelArr)
    {
        [self.db executeUpdate:insertSQLStatment withArgumentsInArray:@[pro.id,pro.proId,DEFISNUL(pro.diaryUser),DEFISNUL(pro.diaryType),DEFISNUL(pro.diaryTime),DEFISNUL(pro.reviewTime), DEFISNUL(pro.tourInfo),DEFISNUL(pro.checkInfo),DEFISNUL(pro.siteInfo),DEFISNUL(pro.weather),DEFISNUL(pro.superviseInfo),DEFISNUL(pro.followInfo),DEFISNUL(pro.safeInfo),DEFISNUL(pro.otherInfo),DEFISNUL(pro.enterExterCommuInfo),DEFISNUL(pro.meetingInfo),DEFISNUL(pro.reviewInfo),DEFISNUL(pro.reviewScore),DEFISNUL(pro.reviewUsers),DEFISNUL(pro.reviewState),DEFISNUL(pro.fileids),DEFISNUL(pro.diaryDesc),DEFISNUL(pro.extendInfo),DEFISNUL(pro.state),DEFISNUL(pro.createusername),DEFISNUL(pro.createtime),DEFISNUL(pro.createip),DEFISNUL(pro.modifyusername),DEFISNUL(pro.modifyip),DEFISNUL(pro.modifytime),@"",@"",@"",DEFISNUL(pro.jianchaInfo),DEFISNUL(pro.zhiliangInfo),DEFISNUL(pro.jinduInfo),DEFISNUL(pro.cailiaoInfo),DEFISNUL(pro.yinbiInfo),DEFISNUL(pro.shoufawenInfo),DEFISNUL(pro.jianzhengInfo),DEFISNUL(pro.huiyiInfo), DEFISNUL(pro.patrolRecordInfo), DEFISNUL(pro.safeRecordInfo)]];
    }
    
    [self.db commit];
    
    [self.db close];
}

// 存储日志表
- (void)saveWorkLog:(NSMutableArray *)workLogArray{
    NSMutableArray *modelArr=[NSMutableArray array];
    for (int i=0; i<workLogArray.count; i++) {
        WorkLog *addressM=[WorkLog yy_modelWithJSON:workLogArray[i]];
        [modelArr addObject:addressM];
    }
    
    [self.db open];
    [self.db setShouldCacheStatements:YES];
    
    //迭代 记录新增类型字段 判断有没有。没有数据库增加此字段。
    if (![self.db columnExists:@"ZHILIANG_INFO" inTableWithName:WORK_LOG]){
        [self.db beginTransaction];
        
        NSString *alertStr1 = [NSString stringWithFormat:@"ALTER TABLE WORK_LOG ADD 'JIANCHA_INFO' TEXT "];
        [self.db executeUpdate:alertStr1];
        
        NSString *alertStr2 = [NSString stringWithFormat:@"ALTER TABLE WORK_LOG ADD 'ZHILIANG_INFO' TEXT"];
        [self.db executeUpdate:alertStr2];
        
        NSString *alertStr3 = [NSString stringWithFormat:@"ALTER TABLE WORK_LOG ADD 'JINDU_INFO' TEXT"];
        [self.db executeUpdate:alertStr3];
        
        NSString *alertStr4 = [NSString stringWithFormat:@"ALTER TABLE WORK_LOG ADD 'CAILIAO_INFO' TEXT"];
        [self.db executeUpdate:alertStr4];
        
        NSString *alertStr5 = [NSString stringWithFormat:@"ALTER TABLE WORK_LOG ADD 'YINBI_INFO' TEXT"];
        [self.db executeUpdate:alertStr5];
        
        NSString *alertStr6 = [NSString stringWithFormat:@"ALTER TABLE WORK_LOG ADD 'SHOUFAWEN_INFO' TEXT"];
        [self.db executeUpdate:alertStr6];
        
        NSString *alertStr7 = [NSString stringWithFormat:@"ALTER TABLE WORK_LOG ADD 'JIANZHENG_INFO' TEXT"];
        [self.db executeUpdate:alertStr7];
        
        NSString *alertStr8 = [NSString stringWithFormat:@"ALTER TABLE WORK_LOG ADD 'HUIYI_INFO' TEXT"];
        [self.db executeUpdate:alertStr8];
        
        [self.db commit];
    }

    static NSString *insertSQLStatment = @"INSERT OR REPLACE INTO WORK_LOG ('ID','PRO_ID','LOG_TYPE','LOG_USERS','REVIEW_TIME','CONSTRACT_PROGRESS_INFO','DAY_SUPERVISION_WORK_INFO','OTHER_SUPERVISION_INFO','SAFE_PATROL_INFO','SAFE_FILE_REVIEW_INFO','SAFE_MEETING_INFO','PROBLEM_DEAL_INFO','REVIEW_INFO','REVIEW_SCORE','REVIEW_USERS','REVIEW_STATE','FILEIDS','LOG_DESC','WEATHER','STATE','EXTEND_INFO','CREATE_USER_NAME','CREATE_TIME','CREATE_IP','MODIFY_USER_NAME','MODIFY_IP','MODIFY_TIME','LOCAL_MODIFY_USER_NAME','LOCAL_MODIFY_TIME','LOCAL_MODIFY_STATE','JIANCHA_INFO','ZHILIANG_INFO','JINDU_INFO','CAILIAO_INFO','YINBI_INFO','SHOUFAWEN_INFO','JIANZHENG_INFO','HUIYI_INFO', 'PATROL_RECORD_INFO', 'SAFE_RECORD_INFO') VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?,?,?,?,?,?,?,?, ?, ?)";
    [self.db beginTransaction];
    
    for (WorkLog *pro in modelArr)
    {
        [self.db executeUpdate:insertSQLStatment withArgumentsInArray:@[pro.id,pro.proId,DEFISNUL(pro.logType),DEFISNUL(pro.logUsers),DEFISNUL(pro.reviewTime),DEFISNUL(pro.constractProgressInfo),DEFISNUL(pro.daySupervisionWorkInfo),DEFISNUL(pro.otherSupervisionInfo),DEFISNUL(pro.safePatrolInfo),DEFISNUL(pro.safeFileReviewInfo),DEFISNUL(pro.safeMeetingInfo),DEFISNUL(pro.problemDealInfo),DEFISNUL(pro.reviewInfo),DEFISNUL(pro.reviewScore),DEFISNUL(pro.reviewUsers),DEFISNUL(pro.reviewState),DEFISNUL(pro.fileids),DEFISNUL(pro.logDesc),DEFISNUL(pro.weather),DEFISNUL(pro.state),DEFISNUL(pro.extendInfo),DEFISNUL(pro.createusername),DEFISNUL(pro.createtime),DEFISNUL(pro.createip),DEFISNUL(pro.modifyUserName),DEFISNUL(pro.modifyIp),DEFISNUL(pro.modifytime),@"",@"",@"",DEFISNUL(pro.jianchaInfo),DEFISNUL(pro.zhiliangInfo),DEFISNUL(pro.jinduInfo),DEFISNUL(pro.cailiaoInfo),DEFISNUL(pro.yinbiInfo),DEFISNUL(pro.shoufawenInfo),DEFISNUL(pro.jianzhengInfo),DEFISNUL(pro.huiyiInfo), DEFISNUL(pro.patrolRecordInfo), DEFISNUL(pro.safeRecordInfo)]];
    }
    
    [self.db commit];
    [self.db close];
}

// 存储记录表
- (void)saveWorkRecord:(NSMutableArray *)workRecordArray {
    NSMutableArray *modelArr=[NSMutableArray array];
    for (int i=0; i<workRecordArray.count; i++) {
        WorkRecord *addressM=[WorkRecord yy_modelWithJSON:workRecordArray[i]];
        [modelArr addObject:addressM];
    }
    
    [self.db open];
    [self.db setShouldCacheStatements:YES];
    
    static NSString *insertSQLStatment = @"INSERT OR REPLACE INTO WORK_RECORD ('ID','PRO_ID','RECORD_USER','RECORD_MASTER','RECORD_SUPERVISION_USER','WEATHER','ADDRESS_ID','ADDRES_INFO','RECORD_TYPE','RECORD_TITLE','RECORDONTENT','RECORD_FILE_PATH','RECORD_INFO','STATE','RECORD_NUMBER','EXTEND_INFO','SUPERVISOR_PRO_UNIT_ID','CONSTRUCT_PRO_UNIT_ID','SUPERVISOR_PRO_UNIT_NAME','CONSTRUCT_PRO_UNIT_NAME','SUPERVISOR_CONTRACT_NUMBER','CONSTRUCT_CONTRACT_NUMBER','SITE_NAME','RANGE_AND_POSITION','CREATE_USER_NAME','CREATE_TIME','CREATE_IP','MODIFY_USER_NAME','MODIFY_IP','MODIFY_TIME','LOCAL_MODIFY_USER_NAME','LOCAL_MODIFY_TIME','LOCAL_MODIFY_STATE', 'PRO_NAME', 'SIDE_START_TIME', 'SIDE_END_TIME') VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    
    [self.db beginTransaction];
    
    for (WorkRecord *pro in modelArr)
    {
        [self.db executeUpdate:insertSQLStatment withArgumentsInArray:@[pro.id,pro.proId,DEFISNUL(pro.recordUser),DEFISNUL(pro.recordMaster),DEFISNUL(pro.recordSupervisionUsers),DEFISNUL(pro.weather),DEFISNUL(pro.addId),DEFISNUL(pro.addInfo),DEFISNUL(pro.recordType),DEFISNUL(pro.recordTitle),DEFISNUL(pro.recordContent),DEFISNUL(pro.recordFilepath),DEFISNUL(pro.recordInfo),DEFISNUL(pro.state),DEFISNUL(pro.recordNumber),DEFISNUL(pro.extendInfo), DEFISNUL(pro.supervisorProUnitId),DEFISNUL(pro.constructProUnitId),DEFISNUL(pro.supervisorProUnitName),DEFISNUL(pro.constructProUnitName),DEFISNUL(pro.supervisorContractNumber),DEFISNUL(pro.constructContractNumber),DEFISNUL(pro.siteName),DEFISNUL(pro.rangeAndPosition), DEFISNUL(pro.createusername),DEFISNUL(pro.createtime),DEFISNUL(pro.createip),DEFISNUL(pro.modifyusername),DEFISNUL(pro.modifyip),DEFISNUL(pro.modifytime),@"",@"",@"", DEFISNUL(pro.projectName), DEFISNUL(pro.sideStartTime), DEFISNUL(pro.sideEndTime)]];
    }
    [self.db commit];
    [self.db close];
}

// 广告表
-(void)saveAdvertisement:(NSMutableArray *)advertisMent{
    NSMutableArray *modelArr=[NSMutableArray array];
    for (int i=0; i<advertisMent.count; i++) {
        Advertising *addressM=[Advertising yy_modelWithJSON:advertisMent[i]];
        [modelArr addObject:addressM];
    }
    
    [self.db open];
    [self.db setShouldCacheStatements:YES];
    
    static NSString *insertSQLStatment = @"INSERT OR REPLACE INTO ADVERTISING_PICTURE ('ID', 'TITLE', 'TEXT', 'STATE', 'SORT', 'ADVERTISEMENT_DESC','ADVERTISEMENT_TYPE','create_user_name','create_time','create_ip','modify_user_name','modify_ip','MODIFY_TIME','LOCAL_MODIFY_STATE','display_range') VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    
    [self.db beginTransaction];
    //新增display_range字段 判断有没有。没有数据库增加此字段
    if (![self.db columnExists:@"display_range" inTableWithName:ADVERTISING_PICTURE]){
        NSString *alertStr = [NSString stringWithFormat:@"ALTER TABLE ADVERTISING_PICTURE ADD display_range text"];
        [self.db executeUpdate:alertStr];
    }
    
    for (Advertising *pro in modelArr)
    {
        [self.db executeUpdate:insertSQLStatment withArgumentsInArray:@[pro.id,pro.title,DEFISNUL(pro.text),DEFISNUL(pro.state),DEFISNUL(pro.sort),DEFISNUL(pro.advertisementDesc),DEFISNUL(pro.advertisementType),DEFISNUL(pro.createusername),DEFISNUL(pro.createtime),DEFISNUL(pro.createip),DEFISNUL(pro.modifyusername),DEFISNUL(pro.modifyip),DEFISNUL(pro.modifytime),@"",DEFISNUL(pro.displayRange)]];
    }
    [self.db commit];
    [self.db close];
}


-(void)saveAttachment:(NSMutableArray *)attachmentArray andDownLoad:(BOOL)isDown{
    NSMutableArray *modelArr=[NSMutableArray array];
    
    self.exit=NO;
    
    for (int i=0; i<attachmentArray.count; i++) {
        Attachment *addressM = [Attachment yy_modelWithJSON:attachmentArray[i]];
        //下载图片
        [modelArr addObject:addressM];
        //已经存在附件，不下载。
        if(![self isExistFileForFileName:addressM.filename]){
            if(addressM.filename){
                [self.fileDic setObject:addressM.id forKey:addressM.filename];

            }
        }else{
            //NSLog(@"附件存在");
        }
    }
    
    [self.db open];
    
    [self.db setShouldCacheStatements:YES];
    
    static NSString *insertSQLStatment = @"INSERT OR REPLACE INTO ATTACHMENT ('ID','FILE_TYPE_ID','FILE_ID','PRO_ID','FILE_NAME','FILE_SIZE','FILE_SUFFIX','FILE_PATH','FILE_DESC','FILE_STATE','EXTEND_INFO','CREATE_USER_NAME','CREATE_TIME','CREATE_IP','MODIFY_USER_NAME','MODIFY_IP','MODIFY_TIME','LOCAL_MODIFY_USER_NAME','LOCAL_MODIFY_TIME','LOCAL_MODIFY_STATE') VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    
    [self.db beginTransaction];
    
    for (Attachment *pro in modelArr)
    {
        [self.db executeUpdate:insertSQLStatment withArgumentsInArray:@[pro.id,pro.fileTypeId,DEFISNUL(pro.fileid),DEFISNUL(pro.proId),DEFISNUL(pro.filename),DEFISNUL(pro.filesize),DEFISNUL(pro.filesuffix),DEFISNUL(pro.filepath),DEFISNUL(pro.fileDesc),DEFISNUL(pro.fileState),DEFISNUL(pro.extendInfo),DEFISNUL(pro.createusername),DEFISNUL(pro.createtime),DEFISNUL(pro.createip),DEFISNUL(pro.modifyusername),DEFISNUL(pro.modifyip),DEFISNUL(pro.modifytime),@"",@"",@""]];
    }
    
    [self.db commit];
    [self.db close];
    
}

/*
 *  同步附件下载
 */
-(void)syncFileImage {
    if(_downThread){
        _downThread=nil;
    }
    _downThread=[[NSThread alloc]initWithTarget:self selector:@selector(downloadFileFromUpdate) object:nil];
    [_downThread start];
}

/*
 *  同步附件下载2
 */
-(void)syncFileImage2{
    [self setMBProgressHudView:@"数据同步中..."];
    [self.hud show:YES];
    
    if(_downThread){
        _downThread=nil;
    }
    _downThread=[[NSThread alloc]initWithTarget:self selector:@selector(downloadFileFromUpdate) object:nil];
    [_downThread start];
}

#pragma mark-- 图片下载
- (void)downLoadImage:(NSString *)imageId andFileName:(NSString *)fileName andJumpBar:(BOOL)isJump{
    //新增一个fileName的参数 链接为空
    NSString *urlStr=[URL_DOWNLOAD stringByAppendingString:[NSString stringWithFormat:@"?localdata=%@&fileName=%@",imageId,fileName]];
    NSURL *URL = [NSURL URLWithString:urlStr];
    
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    
    //添加header
    NSMutableURLRequest *mutableRequest = [request mutableCopy];
    //拷贝request
    [mutableRequest addValue:@"IOS" forHTTPHeaderField:@"app_type"];
    NSString *postStr=DEF_PERSISTENT_GET_OBJECT(@"loginuserPost");
    if(postStr!=nil &&![postStr isEqualToString:@""]){
        [mutableRequest addValue:postStr forHTTPHeaderField:@"username"];
    }
    //拷贝回去
    request = [mutableRequest copy];
    _downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
        
        NSString *path= DEF_FILE_PATH;
        
        NSString *filePath=[path stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@",fileName]];
        
        return [NSURL fileURLWithPath:filePath];
        
    } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
        
        self.completedCount ++ ;
        self.value=self.completedCount*1.0/self.sumCompletedCount;
        
        //判断附件大小是否为0K
        NSString *strUrl=[filePath absoluteString];
        NSArray *strArr=[strUrl componentsSeparatedByString:@"file://"];
        if(strArr.count>=2){
            CGFloat size=  [LSUtility fileSizeAtPath:strArr[1]];
            if(size==0.00){
                //附件表状态改为 mod
                DBHelper *helper=[DBHelper help];
                [helper DataBase:self.db updateTable:ATTACHMENT setKeyValues:@{@"LOCAL_MODIFY_STATE":@"other"} whereCondition:@{@"ID":imageId}];
            }
        }
        
        //是否跳转
        if(isJump)
        {
            dispatch_async(dispatch_get_main_queue(), ^{
                self.progress.progress=self.value;
                self.lab.text=[NSString stringWithFormat:@"%li/%li",self.completedCount,self.sumCompletedCount];
                if(self.value>=1){
                    [self.hud hide:YES];
                    if(self.blockMainTab){
                        self.blockMainTab();
                    }
                }
            });
        }
        else
        {
            dispatch_async(dispatch_get_main_queue(), ^{
                self.progress.progress=self.value;
                self.lab.text=[NSString stringWithFormat:@"%li/%li",self.completedCount,self.sumCompletedCount];
                if(self.value>=1){
                    [self.hud hide:YES];
                    if(self.blockSyncOver){
                        self.blockSyncOver();
                    }
                }
            });
        }
    }];
    [_downloadTask resume];
}


#pragma mark-- 附件下载 PDF 或者是 图片
- (void)downLoadFilePdf:(NSString *)fileId andFileName:(NSString *)fileName andView:(UIView *)view AndFileType:(NSString *)fileType{
    
    [LSUtility showMBProgress:view message:@"下载中"];
    
    NSString *urlStr=[URL_DdownloadPdf stringByAppendingString:[NSString stringWithFormat:@"?id=%@",fileId]];
    NSLog(@"%@",urlStr);
    NSURL *URL = [NSURL URLWithString:urlStr];
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    _downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
        
        self.value=1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount;
        NSLog(@"%.2f", self.value);
        
    } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
        
        NSString *path= DEF_FILE_PDF_PATH;
        NSString *filePath;
        if([fileType isEqualToString:@"pdf"]||[fileType isEqualToString:@"PDF"]){
            filePath=[path stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@+%@.pdf",fileId,fileName]];
        }else{
              filePath=[path stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@+%@.png",fileId,fileName]];
        }
        
        return [NSURL fileURLWithPath:filePath];
        
    } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
        [LSUtility hideMBProgressNOAnimation:view];
        
        if(error){
            NSLog(@"下载错误：%@",error);
            //[self.hud hide:YES];
            //删除文件
            NSFileManager *fileManager = [NSFileManager defaultManager];
            if ([fileManager removeItemAtURL:filePath error:nil]) {
                NSLog(@"删除PDF成功");
            }else{
                NSLog(@"删除PDF失败");
            }
            [LSUtility showToastWithMessage:@"下载失败，请检查文件是否正确！" inView:view Andtime:2.2];
        }else{
            NSLog(@"下载成功");
            dispatch_async(dispatch_get_main_queue(), ^{
                //                NSLog(@"%.2f",self.value);
                self.progress.progress=self.value;

                [LSUtility hideMBProgressNOAnimation:view];
                
                if(self.blockMainTab){
                    self.blockMainTab();
                }
                //                }
            });
            
        }
        
    }];
    
    [_downloadTask resume];
}

/*
 *  判断附件是否存在，存在跳过下载。
 **/
- (BOOL)isExistFileForFileName:(NSString *)fileName{
    //得到图片存储路径
    NSString *path= DEF_FILE_PATH;
    
    //图片存放目录
    NSString *filePath=[path stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@",fileName]];
    // 判断文件是否存在
    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
        return YES;
    }
    
    return NO;
}

#pragma  mark- 创建图片文件夹
- (void) createFile{
    NSString *createPath = DEF_FILE_PATH;
    NSFileManager *fileManager = [[NSFileManager alloc] init];
    // 判断文件夹是否存在，如果不存在，则创建
    if (![[NSFileManager defaultManager] fileExistsAtPath:createPath]) {
        [fileManager createDirectoryAtPath:createPath withIntermediateDirectories:YES attributes:nil error:nil];
    }
}

#pragma  mark- 创建弹出view
- (void)setMBProgressHudView:(NSString *)msg{
    // 设置进度条的百分比
    UIWindow *myWindow=[UIApplication sharedApplication].keyWindow;
    self.hud= [[MBProgressHUD alloc]init];
    self.hud.mode = MBProgressHUDModeCustomView;
    self.hud.margin=0.;
    [myWindow addSubview:self.hud];
    
    self.hud.backgroundColor=[[UIColor blackColor]colorWithAlphaComponent:0.25];
    
    //自定义视图
    UIView *backView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 300*BIZ, 160*BIZ)];
    backView.backgroundColor=[UIColor whiteColor];
    backView.layer.cornerRadius=8*BIZ;
    
    //进度条
    self.progress.backgroundColor=[UIColor colorWithRed:0.15 green:0.72 blue:0.90 alpha:1.00];
    self.progress.trackTintColor=[UIColor colorWithRed:0.66 green:0.66 blue:0.66 alpha:1.00];
    [backView addSubview:self.progress];
    [self.progress mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(backView.mas_centerX);
        make.centerY.mas_equalTo(backView.mas_centerY);
        make.width.mas_equalTo(backView.mas_width).multipliedBy(0.9);
        make.height.offset(2);
    }];
    
    //提示文字
    UILabel *title=[[UILabel alloc]init];
    title.textColor=[UIColor colorWithRed:0.29 green:0.71 blue:0.87 alpha:1.00];
    title.font=[UIFont systemFontOfSize:18*BIZ];
    [backView addSubview:title];
    title.textAlignment=1;
    title.text=msg;
    [title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(10);
        make.left.mas_offset(10);
        make.right.mas_offset(-10);
        make.height.mas_equalTo(backView.mas_height).multipliedBy(0.25);
    }];
    
    //分割线
    UIView *line1=[[UIView alloc]init];
    line1.backgroundColor=[UIColor colorWithRed:0.15 green:0.72 blue:0.89 alpha:1.00];
    [backView addSubview:line1];
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(title.mas_bottom).offset(1);
        make.left.mas_offset(0);
        make.right.mas_offset(0);
        make.height.mas_offset(1);
    }];
    
    //进度显示
    [backView addSubview:self.lab];
    self.lab.textColor=[UIColor colorWithRed:0.29 green:0.71 blue:0.87 alpha:1.00];
    self.lab.font=[UIFont systemFontOfSize:14*BIZ];
    [self.lab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_progress.mas_left);
        make.top.mas_equalTo(_progress.mas_bottom).mas_offset(3);
        make.right.mas_equalTo(_progress.mas_right);
        make.height.offset(20*BIZ);
    }];
    
    WEAKSELF
    //文字信息
    UILabel *detailLab=[[UILabel alloc]init];
    detailLab.text=@"请勿锁屏或断开网络...";
    detailLab.textAlignment=1;
    detailLab.textColor=UIColorFromRGB(0x787878);
    [backView addSubview:detailLab];
    detailLab.font=[UIFont systemFontOfSize:14*BIZ];
    [detailLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(backView);
        make.top.mas_equalTo(weakSelf.lab.mas_bottom).mas_offset(3);
        make.width.offset(200*BIZ);
        make.height.offset(20*BIZ);
    }];
    
    //分割线
    UIView *line=[[UIView alloc]init];
    line.backgroundColor=[UIColor colorWithRed:0.94 green:0.94 blue:0.94 alpha:1.00];
    //    [backView addSubview:line];
    //    [line mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.left.mas_offset(0);
    //        make.right.mas_offset(0);
    //        make.height.mas_offset(1);
    //        make.centerY.multipliedBy(1.5);
    //    }];
    
    //退出按钮
    //    UIButton *btn=[[UIButton alloc]init];
    //    [btn setTitle:@"退  出" forState:UIControlStateNormal];
    //    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    //注释掉此句 去掉退出按钮
    //    [backView addSubview:btn];
    //    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.left.mas_offset(0);
    //        make.right.mas_offset(0);
    //        make.top.mas_equalTo(line.mas_bottom).offset(0);
    //        make.bottom.mas_offset(0);
    //    }];
    
    //
    //    [btn addTarget:self action:@selector(cancelDownLoad) forControlEvents:UIControlEventTouchUpInside];
    
    self.hud.customView=backView;
}

#pragma mark -下载附件(初始化)
- (void)downloadFile{
    /*
     *  下载附件
     */
    if(self.fileDic.count>0){
        //总共附件数量
        self.sumCompletedCount=(int)self.fileDic.count;
        
        NSArray *keyArr=[self.fileDic allKeys];
        for(int i=0;i<keyArr.count;i++){
            if([NSThread currentThread].isCancelled){
                [NSThread exit];
            }
            [self downLoadImage:[self.fileDic objectForKey:keyArr[i]] andFileName:keyArr[i] andJumpBar:YES];
            
            [NSThread sleepForTimeInterval:0.5];
        }
        
    }else {
        dispatch_async(dispatch_get_main_queue(), ^{
            //跳转
            if(self.blockMainTab){
                self.blockMainTab();
            }
        });
        self.hud.hidden=YES;
    }
}

#pragma mark -下载附件(数据更新)
- (void)downloadFileFromUpdate{
    if(self.fileDic.count>0){
        //总共附件数量
        self.sumCompletedCount=(int)self.fileDic.count;
        NSArray *keyArr=[self.fileDic allKeys];
        for(int i=0;i<keyArr.count;i++){
            if([NSThread currentThread].isCancelled){
                [NSThread exit];
            }
            [self downLoadImage:[self.fileDic objectForKey:keyArr[i]] andFileName:keyArr[i] andJumpBar:NO];
        }
        
    }else {
        
        [self.hud hide:YES];
    }
    
}

#pragma  mark -停止下载 取消初始化数据
- (void)cancelDownLoad{
    [self.hud hide:YES];
    [_downThread cancel];
    NSString *userName=DEF_PERSISTENT_GET_OBJECT(@"loginUser");
    NSString *deviceId=DEF_PERSISTENT_GET_OBJECT(@"deviceId");
    if(userName&&deviceId){
        [LSHttpTool Post:URL_LOGIN parameters:@{@"username":DEF_PERSISTENT_GET_OBJECT(@"loginUser"),@"phoneId":DEF_PERSISTENT_GET_OBJECT(@"deviceId"),@"loginState":@"false"} success:^(id responseObject) {
            NSLog(@"%@",responseObject);
        } failure:^(NSError *error) {
            //            [LSUtility showToastWithMessage:@"退出发生错误，请稍后再试。" inView:self.view Andtime:1.5];
        }];
    }
}

#pragma mark -- 二期往下写
// 见证取样流程表
- (void)saveSampleProcess:(NSMutableArray *)sampleArray {
    NSMutableArray *modelArr = [NSMutableArray array];
    for (int i = 0; i < sampleArray.count; i++) {
        SampleProcess *addressM = [SampleProcess yy_modelWithJSON:sampleArray[i]];
        [modelArr addObject:addressM];
    }
    
    [self.db open];
    [self.db setShouldCacheStatements:YES];
    
    static NSString *insertSQLStatment = @"INSERT OR REPLACE INTO SAMPLE_PROCESS ('ID','PRO_ID','PROCESS_NUMBER','SAMPLE_NAME','SAMPLE_DESC','SAMPLE_NUMBER','SAMPLE_MENUFACTURER','SAMPLE_VARIETY','SAMPLE_OUT_GROUND_NUMBER','SAMPLE_VARIETY_MODEL','SAMPLE_IN_TIME','SAMPLE_IN_AMOUNT','SAMPLE_REPRESENT_AMOUT','SAMPLE_GROUP_AMOUNT','SAMPLE_GET_TIME','SAMPLE_GET_ADDRESS', 'SAMPLE_GET_ADDRESS_INFO','SAMPLE_USE_POSITION','SAMPLE_PROJECT','SAMPLE_PRODUCT_STANDARD','SAMPLE_CHECK_STANDARD','SAMPLE_DESIGN_REQUIRED','SAMPLE_GET_USER','SAMPLE_WINESS_USER','SAMPLE_CHECK_USER','SAMPLE_CHECK_UNIT','SAMPLE_QR_CODE','SAMPLE_SECURITY_CODE','SAMPLE_PROCESS_STATE','SAMPLE_PROCESS_DESC','EXTEND_INFO','CREATE_USER_NAME','CREATE_TIME','CREATE_IP','MODIFY_USER_NAME','MODIFY_IP','MODIFY_TIME','LOCAL_MODIFY_USER_NAME','LOCAL_MODIFY_TIME','LOCAL_MODIFY_STATE') VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    
    [self.db beginTransaction];
    for (SampleProcess *pro in modelArr)
    {
        [self.db executeUpdate:insertSQLStatment withArgumentsInArray:@[DEFISNUL(pro.id),DEFISNUL(pro.proId),DEFISNUL(pro.processNumber),DEFISNUL(pro.sampleName),DEFISNUL(pro.sampleDesc),DEFISNUL(pro.sampleNumber),DEFISNUL(pro.sampleMenufacturer),DEFISNUL(pro.sampleVariety),DEFISNUL(pro.sampleOutGroundNumber),DEFISNUL(pro.sampleVarietyModel),DEFISNUL(pro.sampleInTime),DEFISNUL(pro.sampleInAmount),DEFISNUL(pro.sampleRepresentAmout),DEFISNUL(pro.sampleGroupAmount),DEFISNUL(pro.sampleGetTime),DEFISNUL(pro.sampleGetAddress), DEFISNUL(pro.sampleGetAddressInfo), DEFISNUL(pro.sampleUsePosition),DEFISNUL(pro.sampleProject),DEFISNUL(pro.sampleProductStandard),DEFISNUL(pro.sampleCheckStandard),DEFISNUL(pro.sampleDesignRequired),DEFISNUL(pro.sampleGetUser),DEFISNUL(pro.sampleWinessUser),DEFISNUL(pro.sampleCheckUser),DEFISNUL(pro.sampleCheckUnit),DEFISNUL(pro.sampleQrCode),DEFISNUL(pro.sampleSecurityCode),DEFISNUL(pro.sampleProcessState),DEFISNUL(pro.sampleProcessDesc),DEFISNUL(pro.extendInfo),DEFISNUL(pro.createusername),DEFISNUL(pro.createtime),DEFISNUL(pro.createip),DEFISNUL(pro.modifyusername),DEFISNUL(pro.modifyip),DEFISNUL(pro.modifytime),@"",@"",@""]];
    }
    [self.db commit];
    [self.db close];
}

// 见证取样流程记录表
- (void)saveSampleRecord:(NSMutableArray *)sampleRecordArray {
    NSMutableArray *modelArr = [NSMutableArray array];
    for (int i = 0; i < sampleRecordArray.count; i++) {
        SampleProcessRecord *addressM = [SampleProcessRecord yy_modelWithJSON:sampleRecordArray[i]];
        [modelArr addObject:addressM];
    }
    
    [self.db open];
    [self.db setShouldCacheStatements:YES];
    
    static NSString *insertSQLStatment = @"INSERT OR REPLACE INTO SAMPLE_PROCESS_RECORD ('ID','PRO_ID','PROCESS_ID','RECORD_TIME','RECORD_USER','RECORD_CONTENT','SAMPLE_WITNESS_RESULT','SAMPLE_WITNESS_SCORE','SAMPLE_WITNESS_REVIEW','SAMPLE_CHECK_TORF_RESULT','SAMPLE_CHECK_TORF_DESC','SAMPLE_CHECK_RESULT','SAMPLE_CHECK_DESC','SAMPLE_CHECK_RESULT_DESC','SAMPLE_CHECK_RESULT_SCORE','SAMPLE_PROCESS_STATE','RECORD_DESC','EXTEND_INFO','CREATE_USER_NAME','CREATE_TIME','CREATE_IP','MODIFY_USER_NAME','MODIFY_IP','MODIFY_TIME','LOCAL_MODIFY_USER_NAME','LOCAL_MODIFY_TIME','LOCAL_MODIFY_STATE') VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    
    [self.db beginTransaction];
    for (SampleProcessRecord *pro in modelArr)
    {
        [self.db executeUpdate:insertSQLStatment withArgumentsInArray:@[DEFISNUL(pro.id),DEFISNUL(pro.proId),DEFISNUL(pro.processId),DEFISNUL(pro.recordTime),DEFISNUL(pro.recordUser),DEFISNUL(pro.recordContent),DEFISNUL(pro.sampleWitnessResult),DEFISNUL(pro.sampleWitnessScore),DEFISNUL(pro.sampleWitnessReview),DEFISNUL(pro.sampleCheckTorfResult),DEFISNUL(pro.sampleCheckTorfDesc),DEFISNUL(pro.sampleCheckResult),DEFISNUL(pro.sampleCheckDesc),DEFISNUL(pro.sampleCheckResultDesc),DEFISNUL(pro.sampleCheckResultScore),DEFISNUL(pro.sampleProcessState),DEFISNUL(pro.recordDesc),DEFISNUL(pro.extendInfo),DEFISNUL(pro.createusername),DEFISNUL(pro.createtime),DEFISNUL(pro.createip),DEFISNUL(pro.modifyusername),DEFISNUL(pro.modifyip),DEFISNUL(pro.modifytime),@"",@"",@""]];
    }
    [self.db commit];
    [self.db close];
}

// 见证取样二维码表
- (void)saveSampleCode:(NSMutableArray *)sampleCodeArray {
    NSMutableArray *modelArr = [NSMutableArray array];
    for (int i = 0; i < sampleCodeArray.count; i++) {
        SampleCode *addressM = [SampleCode yy_modelWithJSON:sampleCodeArray[i]];
        [modelArr addObject:addressM];
    }
    
    [self.db open];
    [self.db setShouldCacheStatements:YES];
    
    static NSString *insertSQLStatment = @"INSERT OR REPLACE INTO SAMPLE_CODE ('ID','SAMPLE_QR_CODE','SAMPLE_SECURITY_CODE','SAMPLE_CODE_SOURCE','SAMPLE_CODE_DESC','SAMPLE_CODE_STATE','EXTEND_INFO','CREATE_USER_NAME','CREATE_TIME','CREATE_IP','MODIFY_USER_NAME','MODIFY_IP','MODIFY_TIME','LOCAL_MODIFY_USER_NAME','LOCAL_MODIFY_TIME','LOCAL_MODIFY_STATE', 'SERIES_NUMBER') VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    
    [self.db beginTransaction];
    for (SampleCode *pro in modelArr)
    {
        [self.db executeUpdate:insertSQLStatment withArgumentsInArray:@[DEFISNUL(pro.id),DEFISNUL(pro.sampleQrCode),DEFISNUL(pro.sampleSecurityCode),DEFISNUL(pro.sampleCodeSource),DEFISNUL(pro.sampleCodeDesc),DEFISNUL(pro.sampleCodeState), DEFISNUL(pro.extendInfo),DEFISNUL(pro.createusername),DEFISNUL(pro.createtime),DEFISNUL(pro.createip),DEFISNUL(pro.modifyusername),DEFISNUL(pro.modifyip),DEFISNUL(pro.modifytime),@"",@"",@"", pro.seriesNumber]];
    }
    [self.db commit];
    [self.db close];
}

// 巡检流程
- (void)saveInspectProcess:(NSMutableArray *)inspectProcessArray {
    NSMutableArray *modelArr = [NSMutableArray array];
    for (int i = 0; i < inspectProcessArray.count; i++) {
        InspectProcess *addressM = [InspectProcess yy_modelWithJSON:inspectProcessArray[i]];
        [modelArr addObject:addressM];
    }
    
    [self.db open];
    [self.db setShouldCacheStatements:YES];
    
    static NSString *insertSQLStatment = @"INSERT OR REPLACE INTO INSPECT_PROCESS ('ID','PRO_ID','INSPECT_GROUP_ID','INSPECT_NAME','INSPECT_START_TIME','INSPECT_END_TIME','INSPECT_PUBLISHER','INSPECT_RECTIFICATE_LEADER','INSPECT_GROUP_LEADER','INSPECT_STATE','INSPECT_DESC','INSPECT_TOTAL_DESC','EXTEND_INFO','CREATE_USER_NAME','CREATE_TIME','CREATE_IP','MODIFY_USER_NAME','MODIFY_IP','MODIFY_TIME','LOCAL_MODIFY_USER_NAME','LOCAL_MODIFY_TIME','LOCAL_MODIFY_STATE') VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    
    [self.db beginTransaction];
    for (InspectProcess *pro in modelArr)
    {
        [self.db executeUpdate:insertSQLStatment withArgumentsInArray:@[DEFISNUL(pro.id),DEFISNUL(pro.proId),DEFISNUL(pro.inspectGroupId),DEFISNUL(pro.inspectName),DEFISNUL(pro.inspectStartTime),DEFISNUL(pro.inspectEndTime),DEFISNUL(pro.inspectPublisher),DEFISNUL(pro.inspectRectificateLeader),DEFISNUL(pro.inspectGroupLeader),DEFISNUL(pro.inspectState),DEFISNUL(pro.inspectDesc),DEFISNUL(pro.inspectTotalDesc),DEFISNUL(pro.extendInfo),DEFISNUL(pro.createusername),DEFISNUL(pro.createtime),DEFISNUL(pro.createip),DEFISNUL(pro.modifyusername),DEFISNUL(pro.modifyip),DEFISNUL(pro.modifytime),@"",@"",@""]];
    }
    [self.db commit];
    [self.db close];
}

// 巡检流程记录
- (void)saveInspectProcessRecord:(NSMutableArray *)inspectRecordArray {
    NSMutableArray *modelArr = [NSMutableArray array];
    for (int i = 0; i < inspectRecordArray.count; i++) {
        InspectProcessRecord *addressM = [InspectProcessRecord yy_modelWithJSON:inspectRecordArray[i]];
        [modelArr addObject:addressM];
    }
    
    [self.db open];
    [self.db setShouldCacheStatements:YES];
    
    static NSString *insertSQLStatment = @"INSERT OR REPLACE INTO INSPECT_PROCESS_RECORD ('ID','PRO_ID','INSPECT_PROCESS_ID','INSPECT_USER_ID','INSPECT_ITEM_ID','INSPECT_USER_ITEM_ID','INSPECT_ITEM_STATE','INSPECT_INFO','INSPECT_DESC','INSPECT_RESULT','INSPECT_SCORE','INSPECT_ITEM_RECIFICATE_LEADER','INSPECT_RECIFICATE_DESC','INSPECT_RECIFICATE_REVIEW_USER','INSPECT_RECIFICATE_REVIRE_SCORE','INSPECT_RECIFICATE_REVIEW','INSPECT_DO_TIME','EXTEND_INFO','CREATE_USER_NAME','CREATE_TIME','CREATE_IP','MODIFY_USER_NAME','MODIFY_IP','MODIFY_TIME','LOCAL_MODIFY_USER_NAME','LOCAL_MODIFY_TIME','LOCAL_MODIFY_STATE') VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?, ?, ?, ?, ?, ?,?, ?, ?, ?, ?, ?)";
    
    [self.db beginTransaction];
    for (InspectProcessRecord *pro in modelArr)
    {
        [self.db executeUpdate:insertSQLStatment withArgumentsInArray:@[DEFISNUL(pro.id),DEFISNUL(pro.proId),DEFISNUL(pro.inspectProcessId),DEFISNUL(pro.inspectUserId),DEFISNUL(pro.inspectItemId),DEFISNUL(pro.inspectUserItemId),DEFISNUL(pro.inspectItemState),DEFISNUL(pro.inspectInfo),DEFISNUL(pro.inspectDesc),DEFISNUL(pro.inspectResult),DEFISNUL(pro.inspectScore),DEFISNUL(pro.inspectItemRecificateLeader),DEFISNUL(pro.inspectRecificateDesc),DEFISNUL(pro.inspectRecificateReviewUser),DEFISNUL(pro.inspectRecificateRevireScore),DEFISNUL(pro.inspectRecificateReview),DEFISNUL(pro.inspectDoTime),DEFISNUL(pro.extendInfo),DEFISNUL(pro.createusername),DEFISNUL(pro.createtime),DEFISNUL(pro.createip),DEFISNUL(pro.modifyusername),DEFISNUL(pro.modifyip),DEFISNUL(pro.modifytime),@"",@"",@""]];
    }
    [self.db commit];
    [self.db close];
}

// 巡检项
- (void)saveInspectItem:(NSMutableArray *)inspectItemArray {
    NSMutableArray *modelArr = [NSMutableArray array];
    for (int i = 0; i < inspectItemArray.count; i++) {
        InspectItem *addressM = [InspectItem yy_modelWithJSON:inspectItemArray[i]];
        [modelArr addObject:addressM];
    }
    
    [self.db open];
    [self.db setShouldCacheStatements:YES];
    
    static NSString *insertSQLStatment = @"INSERT OR REPLACE INTO INSPECT_ITEM ('ID','PARENT_ID','INSPECT_NAME','INSPECT_SHORT_NAME','INSPECT_NUMBER','INSPECT_SERIOURS_LEVEL','INSPECT_SCORE','INSPECT_TITLE','INSPECT_DESC','EXTEND_INFO','CREATE_USER_NAME','CREATE_TIME','CREATE_IP','MODIFY_USER_NAME','MODIFY_IP','MODIFY_TIME','LOCAL_MODIFY_USER_NAME','LOCAL_MODIFY_TIME','LOCAL_MODIFY_STATE') VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    
    [self.db beginTransaction];
    for (InspectItem *pro in modelArr)
    {
        [self.db executeUpdate:insertSQLStatment withArgumentsInArray:@[DEFISNUL(pro.id),DEFISNUL(pro.parentId),DEFISNUL(pro.inspectName),DEFISNUL(pro.inspectShortName),DEFISNUL(pro.inspectNumber),DEFISNUL(pro.inspectSerioursLevel),DEFISNUL(pro.inspectScore),DEFISNUL(pro.inspectTitle),DEFISNUL(pro.inspectDesc),DEFISNUL(pro.extendInfo),DEFISNUL(pro.createusername),DEFISNUL(pro.createtime),DEFISNUL(pro.createip),DEFISNUL(pro.modifyusername),DEFISNUL(pro.modifyip),DEFISNUL(pro.modifytime),@"",@"",@""]];
    }
    [self.db commit];
    [self.db close];
}

// 用户巡检项
- (void)saveInspectUserItem:(NSMutableArray *)inspectUserItemArray {
    NSMutableArray *modelArr = [NSMutableArray array];
    for (int i = 0; i < inspectUserItemArray.count; i++) {
        InspectUserItem *addressM = [InspectUserItem yy_modelWithJSON:inspectUserItemArray[i]];
        [modelArr addObject:addressM];
    }
    
    [self.db open];
    [self.db setShouldCacheStatements:YES];
    
    static NSString *insertSQLStatment = @"INSERT OR REPLACE INTO INSPECT_USER_ITEM ('ID', 'INSPECT_PROCESS_ID', 'INSPECT_GROUP_ID', 'INSPECT_USER_ID', 'INSPECT_USER_TYPE', 'INSPECT_USER_DESC', 'INSPECT_ITEM_ID', 'INSPECT_ADDTIONAL_NAME', 'INSPECT_CURRENT_STATE', SUPER_PROCESS_ID, INSPECT_DESC, 'EXTEND_INFO', 'CREATE_USER_NAME', 'CREATE_TIME', 'CREATE_IP', 'MODIFY_USER_NAME', 'MODIFY_TIME', 'MODIFY_IP', 'LOCAL_MODIFY_USER_NAME', 'LOCAL_MODIFY_TIME', 'LOCAL_MODIFY_STATE') VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    
    [self.db beginTransaction];
    for (InspectUserItem *pro in modelArr)
    {
        [self.db executeUpdate:insertSQLStatment withArgumentsInArray:@[DEFISNUL(pro.id), DEFISNUL(pro.inspectProcessId), DEFISNUL(pro.inspectGroupId), DEFISNUL(pro.inspectUserId), DEFISNUL(pro.inspectUserType), DEFISNUL(pro.inspectUserDesc), DEFISNUL(pro.inspectItemId), DEFISNUL(pro.inspectAddtionalName), DEFISNUL(pro.inspectCurrentState), DEFISNUL(pro.superProcessId), DEFISNUL(pro.inspectDesc), DEFISNUL(pro.extendInfo), DEFISNUL(pro.createusername), DEFISNUL(pro.createtime), DEFISNUL(pro.createip), DEFISNUL(pro.modifyusername), DEFISNUL(pro.modifytime), DEFISNUL(pro.modifyip), @"", @"", @""]];
    }
    [self.db commit];
    [self.db close];
}

// 巡检组
- (void)saveInspectGroup:(NSMutableArray *)inspectGroupArray {
    NSMutableArray *modelArr = [NSMutableArray array];
    for (int i = 0; i < inspectGroupArray.count; i++) {
        InspectGroup *addressM = [InspectGroup yy_modelWithJSON:inspectGroupArray[i]];
        [modelArr addObject:addressM];
    }
    
    [self.db open];
    [self.db setShouldCacheStatements:YES];
    
    static NSString *insertSQLStatment = @"INSERT OR REPLACE INTO INSPECT_GROUP ('ID','INSPECT_GROUP_NAME','INSPECT_GROUP_SHORT_NAME','INSPECT_GROUP_NUMBER','INSPECT_GROUP_DESC','EXTEND_INFO','CREATE_USER_NAME','CREATE_TIME','CREATE_IP','MODIFY_USER_NAME','MODIFY_IP','MODIFY_TIME','LOCAL_MODIFY_USER_NAME','LOCAL_MODIFY_TIME','LOCAL_MODIFY_STATE') VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    
    [self.db beginTransaction];
    for (InspectGroup *pro in modelArr)
    {
        [self.db executeUpdate:insertSQLStatment withArgumentsInArray:@[DEFISNUL(pro.id),DEFISNUL(pro.inspectGroupName),DEFISNUL(pro.inspectGroupShortName),DEFISNUL(pro.inspectGroupNumber),DEFISNUL(pro.inspectGroupDesc),DEFISNUL(pro.extendInfo),DEFISNUL(pro.createusername),DEFISNUL(pro.createtime),DEFISNUL(pro.createip),DEFISNUL(pro.modifyusername),DEFISNUL(pro.modifyip),DEFISNUL(pro.modifytime),@"",@"",@""]];
    }
    [self.db commit];
    [self.db close];
}

// 巡检组用户表
- (void)saveInspectGroupUser:(NSMutableArray *)inspectGroupUserArray {
    NSMutableArray *modelArr = [NSMutableArray array];
    for (int i = 0; i < inspectGroupUserArray.count; i++) {
        InspectGroupUser *addressM = [InspectGroupUser yy_modelWithJSON:inspectGroupUserArray[i]];
        [modelArr addObject:addressM];
    }
    
    [self.db open];
    
    [self.db setShouldCacheStatements:YES];
    
    static NSString *insertSQLStatment = @"INSERT OR REPLACE INTO INSPECT_GROUP_USER ('ID','INSPECT_GROUP_ID','INSPECT_USER_ID','INSPECT_USER_TYPE','INSPECT_DESC','EXTEND_INFO','CREATE_USER_NAME','CREATE_TIME','CREATE_IP','MODIFY_USER_NAME','MODIFY_IP','MODIFY_TIME','LOCAL_MODIFY_USER_NAME','LOCAL_MODIFY_TIME','LOCAL_MODIFY_STATE') VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    
    [self.db beginTransaction];
    
    for (InspectGroupUser *pro in modelArr)
    {
        [self.db executeUpdate:insertSQLStatment withArgumentsInArray:@[DEFISNUL(pro.id),DEFISNUL(pro.inspectGroupId),DEFISNUL(pro.inspectUserId),DEFISNUL(pro.inspectUserType),DEFISNUL(pro.inspectDesc),DEFISNUL(pro.extendInfo),DEFISNUL(pro.createusername),DEFISNUL(pro.createtime),DEFISNUL(pro.createip),DEFISNUL(pro.modifyusername),DEFISNUL(pro.modifyip),DEFISNUL(pro.modifytime),@"",@"",@""]];
    }
    [self.db commit];
    [self.db close];
}

// 一键汇总表
- (void)saveInspectTotalRecord:(NSMutableArray *)inspectTotalRecordArray {
    NSMutableArray *modelArr = [NSMutableArray array];
    for (int i = 0; i < inspectTotalRecordArray.count; i++) {
        InspectTotalRecord *addressM = [InspectTotalRecord yy_modelWithJSON:inspectTotalRecordArray[i]];
        [modelArr addObject:addressM];
    }
    
    [self.db open];
    [self.db setShouldCacheStatements:YES];
    
    static NSString *insertSQLStatment = @"INSERT OR REPLACE INTO INSPECT_TOTAL_RECORD ('ID','INSPECT_PROCESS_ID','INSPECT_TOTAL_TIME','INSPECT_TOTAL_INFO','INSPECT_TOTAL_DESC','INSPECT_TOTAL_USER','EXTEND_INFO','CREATE_USER_NAME','CREATE_TIME','CREATE_IP','MODIFY_USER_NAME','MODIFY_IP','MODIFY_TIME','LOCAL_MODIFY_USER_NAME','LOCAL_MODIFY_TIME','LOCAL_MODIFY_STATE') VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    
    [self.db beginTransaction];
    for (InspectTotalRecord *pro in modelArr)
    {
      BOOL istrue =   [self.db executeUpdate:insertSQLStatment withArgumentsInArray:@[DEFISNUL(pro.id),DEFISNUL(pro.inspectProcessId),DEFISNUL(pro.inspectTotalTime),DEFISNUL(pro.inspectTotalInfo),DEFISNUL(pro.inspectTotalDesc),DEFISNUL(pro.inspectTotalUser),DEFISNUL(pro.extendInfo),DEFISNUL(pro.createusername),DEFISNUL(pro.createtime),DEFISNUL(pro.createip),DEFISNUL(pro.modifyusername),DEFISNUL(pro.modifyip),DEFISNUL(pro.modifytime),@"",@"",@""]];
        NSLog(@"%d",istrue);
    }
    [self.db commit];
    [self.db close];
}

// 巡视记录表
- (void)savePatrolRecordArray:(NSMutableArray *)patrolRecordArray {
    
    NSMutableArray *modelArr=[NSMutableArray array];
    for (int i=0; i<patrolRecordArray.count; i++) {
        PatrolRecord *addressM = [PatrolRecord yy_modelWithJSON:patrolRecordArray[i]];
        [modelArr addObject:addressM];
    }
    
    [self.db open];
    [self.db setShouldCacheStatements:YES];
    
    static NSString *insertSQLStatment = @"INSERT OR REPLACE INTO PATROL_RECORD ('ID','PRO_ID','SUPERVISOR_PRO_UNIT_ID','CONSTRUCT_PRO_UNIT_ID','SUPERVISOR_PRO_UNIT_NAME','CONSTRUCT_PRO_UNIT_NAME','SUPERVISOR_CONTRACT_NUMBER','CONSTRUCT_CONTRACT_NUMBER','RECORD_USER','RECORD_TYPE','RECORD_TITLE','RECORD_CONTENT','STATE','RECORD_NUMBER','START_TIME','END_TIME','START_AND_END_TIME','WEATHER','ADD_ID','ADD_INFO','RANGE_AND_POSITION','MAJORPRO_AND_PERSON','MAJOR_INSPECT_DATA','PROBLEM_AND_DEAL','EXTEND_INFO','CREATE_USER_NAME','CREATE_TIME','CREATE_IP','MODIFY_USER_NAME','MODIFY_TIME','MODIFY_IP','LOCAL_MODIFY_USER_NAME','LOCAL_MODIFY_TIME','LOCAL_MODIFY_STATE') VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    
    [self.db beginTransaction];
    
    for (PatrolRecord *ad in modelArr)
    {
        [self.db executeUpdate:insertSQLStatment withArgumentsInArray:@[DEFISNUL(ad.id),DEFISNUL(ad.proId),DEFISNUL(ad.supervisorProUnitId),DEFISNUL(ad.constructProUnitId),DEFISNUL(ad.supervisorProUnitName),DEFISNUL(ad.constructProUnitName),DEFISNUL(ad.supervisorContractNumber),DEFISNUL(ad.constructContractNumber),DEFISNUL(ad.recordUser),DEFISNUL(ad.recordType),DEFISNUL(ad.recordTitle),DEFISNUL(ad.recordContent),DEFISNUL(ad.state),DEFISNUL(ad.recordNumber),DEFISNUL(ad.startTime),DEFISNUL(ad.endTime),DEFISNUL(ad.startAndEndTime),DEFISNUL(ad.weather),DEFISNUL(ad.addId),DEFISNUL(ad.addInfo),DEFISNUL(ad.rangeAndPosition),DEFISNUL(ad.majorproAndPerson),DEFISNUL(ad.majorInspectData),DEFISNUL(ad.problemAndDeal),DEFISNUL(ad.extendInfo),DEFISNUL(ad.createusername),DEFISNUL(ad.createtime),DEFISNUL(ad.createip),DEFISNUL(ad.modifyusername),DEFISNUL(ad.modifytime),DEFISNUL(ad.modifyip),@"",@"",@""]];
    }
    [self.db commit];
    [self.db close];
}

// 安全记录基本表
- (void)saveSecurityRecordBasic:(NSMutableArray *)securityRecordBasicArray {
    
    NSMutableArray *modelArr=[NSMutableArray array];
    for (int i=0; i<securityRecordBasicArray.count; i++) {
        SecurityRecordBasic *addressM=[SecurityRecordBasic yy_modelWithJSON:securityRecordBasicArray[i]];
        [modelArr addObject:addressM];
    }
    
    [self.db open];
    [self.db setShouldCacheStatements:YES];
    
    static NSString *insertSQLStatment = @"INSERT OR REPLACE INTO SAFE_RECORD_BASIC ('ID','PRO_ID','SUPERVISOR_PRO_UNIT_ID','CONSTRUCT_PRO_UNIT_ID','SUPERVISOR_PRO_UNIT_NAME','CONSTRUCT_PRO_UNIT_NAME','SUPERVISOR_CONTRACT_NUMBER','CONSTRUCT_CONTRACT_NUMBER','RECORD_USER','RECORD_TYPE','RECORD_TITLE','RECORD_CONTENT','STATE','RECORD_NUMBER','SUPERVISOR_OPINION','CHIEF_SUPERVISOR_OPINION','PROJECT_CONTENT','OTHER_INFO','WEATHER','ADD_ID','ADD_INFO','EXTEND_INFO','CREATE_USER_NAME','CREATE_TIME','CREATE_IP','MODIFY_USER_NAME','MODIFY_TIME','MODIFY_IP','LOCAL_MODIFY_USER_NAME','LOCAL_MODIFY_TIME','LOCAL_MODIFY_STATE', 'PROBLEM_AND_DEAL', 'CHIEF_SUPERVISOR_OPINION_STATE', 'CONSTRUCT_CONTRACT_MONEY', 'RECIFY_STATE') VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    
    [self.db beginTransaction];
    for (SecurityRecordBasic *ad in modelArr)
    {
        [self.db executeUpdate:insertSQLStatment withArgumentsInArray:@[DEFISNUL(ad.id),DEFISNUL(ad.proId),DEFISNUL(ad.supervisorProUnitId),DEFISNUL(ad.constructProUnitId),DEFISNUL(ad.supervisorProUnitName),DEFISNUL(ad.constructProUnitName),DEFISNUL(ad.supervisorContractNumber),DEFISNUL(ad.constructContractNumber),DEFISNUL(ad.recordUser),DEFISNUL(ad.recordType),DEFISNUL(ad.recordTitle),DEFISNUL(ad.recordContent),DEFISNUL(ad.state),DEFISNUL(ad.recordNumber),DEFISNUL(ad.supervisorOpinion),DEFISNUL(ad.chiefSupervisorOpinion),DEFISNUL(ad.projectContent),DEFISNUL(ad.otherInfo),DEFISNUL(ad.weather),DEFISNUL(ad.addId),DEFISNUL(ad.addInfo), DEFISNUL(ad.extendInfo),DEFISNUL(ad.createusername),DEFISNUL(ad.createtime),DEFISNUL(ad.createip),DEFISNUL(ad.modifyusername),DEFISNUL(ad.modifytime),DEFISNUL(ad.modifyip),@"",@"",@"", DEFISNUL(ad.problemAndDeal), DEFISNUL(ad.chiefSupervisorOpinionState), DEFISNUL(ad.constructContractMoney), DEFISNUL(ad.recifyState)]];
    }
    [self.db commit];
    [self.db close];
}

// 安全记录-施工三类人员
- (void)saveThreeTypePeople:(NSMutableArray *)threeTypePeopleArray {
    NSMutableArray *modelArr=[NSMutableArray array];
    for (int i=0; i<threeTypePeopleArray.count; i++) {
        ThreeTypePeople *addressM=[ThreeTypePeople yy_modelWithJSON:threeTypePeopleArray[i]];
        
        [modelArr addObject:addressM];
    }
    
    [self.db open];
    [self.db setShouldCacheStatements:YES];
    
    static NSString *insertSQLStatment = @"INSERT OR REPLACE INTO SAFE_CONSTRUCT ('ID', 'PRO_ID', 'BASIC_RECORD_ID', 'RECORD_USER', 'RECORD_TYPE', 'RECORD_TITLE', 'RECORD_NUMBER', 'TOTAL_USER_TYPE', 'DANGER_FACTOR', 'USER_TITLE', 'IS_ON_POSITION', 'IS_DO_CHECK', 'IS_EFFECITIVE', 'IS_MEET_REQUIRE', 'OTHER_NOTE', 'PRO_ON_DUTY_CNT', 'PRO_FIT_CNT', 'PRO_FIT_RATE', 'PRO_IS_MEET_REQUIRE', 'SAFE_ONDUTYCNT', 'SAFE_FIT_CNT', 'SAFE_FIT_RATE', 'SAFE_IS_MEET_REQUIRE', 'EXTEND_INFO', 'CREATE_USER_NAME', 'CREATE_TIME', 'CREATE_IP', 'MODIFY_USER_NAME', 'MODIFY_TIME', 'MODIFY_IP', 'LOCAL_MODIFY_USER_NAME', 'LOCAL_MODIFY_TIME', 'LOCAL_MODIFY_STATE', 'USERNAME', 'REALNAME') VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    
    [self.db beginTransaction];
    
    for (ThreeTypePeople *user in modelArr)
    {
        
        [self.db executeUpdate:insertSQLStatment withArgumentsInArray:@[DEFISNUL(user.id), DEFISNUL(user.proId), DEFISNUL(user.basicRecordId), DEFISNUL(user.recordUser), DEFISNUL(user.recordType), DEFISNUL(user.recordTitle), DEFISNUL(user.recordNumber), DEFISNUL(user.totalUserType), DEFISNUL(user.dangerFactor), DEFISNUL(user.userTitle), DEFISNUL(user.isOnPosition), DEFISNUL(user.isDoCheck), DEFISNUL(user.isEffecitive), DEFISNUL(user.isMeetRequire), DEFISNUL(user.otherNote), DEFISNUL(user.proOnDutyCnt), DEFISNUL(user.proFitCnt), DEFISNUL(user.proFitRate), DEFISNUL(user.proIsMeetRequire), DEFISNUL(user.safeOnDutyCnt), DEFISNUL(user.safeFitCnt), DEFISNUL(user.safeFitRate), DEFISNUL(user.safeIsMeetRequire), DEFISNUL(user.extendInfo), DEFISNUL(user.createusername), DEFISNUL(user.createtime), DEFISNUL(user.createip), DEFISNUL(user.modifyusername), DEFISNUL(user.modifytime), DEFISNUL(user.modifyip), @"", @"", @"", DEFISNUL(user.username), DEFISNUL(user.realname)]];
    }
    [self.db commit];
    [self.db close];
}

// 安全记录-特种作业人员
- (void)saveSpecialOperationsPersonnel:(NSMutableArray *)specialOperationsPersonnelArray {
    NSMutableArray *modelArr=[NSMutableArray array];
    for (int i=0; i<specialOperationsPersonnelArray.count; i++) {
        SpecialOperationsPersonnel *addressM=[SpecialOperationsPersonnel yy_modelWithJSON:specialOperationsPersonnelArray[i]];
        [modelArr addObject:addressM];
    }
    
    [self.db open];
    [self.db setShouldCacheStatements:YES];
    
    static NSString *insertSQLStatment = @"INSERT OR REPLACE INTO SAFE_SPECIAL_TYPE ('ID', 'PRO_ID', 'BASIC_RECORD_ID', 'RECORD_USER', 'RECORD_TYPE', 'RECORD_TITLE', 'RECORD_NUMBER', 'CHECK_NAME', 'WORK_TYPE', 'APPLY_CNT', 'CHECK_REAL_CNT', 'EFFECTIVE_CNT', 'IN_EFFECTIVE_CNT', 'FIT_RATE', 'CHECK_OPINION', 'EXTEND_INFO', 'CREATE_USER_NAME', 'CREATE_TIME', 'CREATE_IP', 'MODIFY_USER_NAME', 'MODIFY_TIME', 'MODIFY_IP', 'LOCAL_MODIFY_USER_NAME', 'LOCAL_MODIFY_TIME', 'LOCAL_MODIFY_STATE', 'SOUCE_CHECK_CNT') VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    
    [self.db beginTransaction];
    
    for (SpecialOperationsPersonnel *pro in modelArr)
    {
        [self.db executeUpdate:insertSQLStatment withArgumentsInArray:@[DEFISNUL(pro.id), DEFISNUL(pro.proId), DEFISNUL(pro.basicRecordId), DEFISNUL(pro.recordUser), DEFISNUL(pro.recordType), DEFISNUL(pro.recordTitle), DEFISNUL(pro.recordNumber), DEFISNUL(pro.checkName), DEFISNUL(pro.workType), DEFISNUL(pro.applyCnt), DEFISNUL(pro.checkRealCnt), DEFISNUL(pro.effectiveCnt), DEFISNUL(pro.inEffectiveCnt), DEFISNUL(pro.fitRate), DEFISNUL(pro.checkOpinion), DEFISNUL(pro.extendInfo), DEFISNUL(pro.createusername), DEFISNUL(pro.createtime), DEFISNUL(pro.createip), DEFISNUL(pro.modifyusername), DEFISNUL(pro.modifytime), DEFISNUL(pro.modifyip), @"", @"", @"", DEFISNUL(pro.souceCheckCnt)]];
    }
    [self.db commit];
    [self.db close];
}

// 安全记录-安全巡查
- (void)saveSecurityPatrol:(NSMutableArray *)securityPatrolArray{
    NSMutableArray *modelArr=[NSMutableArray array];
    for (int i=0; i<securityPatrolArray.count; i++) {
        SecurityPatrol *addressM=[SecurityPatrol yy_modelWithJSON:securityPatrolArray[i]];
        [modelArr addObject:addressM];
    }
    
    [self.db open];
    [self.db setShouldCacheStatements:YES];
    
    static NSString *insertSQLStatment = @"INSERT OR REPLACE INTO SECURITY_PATROL ('ID','PRO_ID','BASIC_RECORD_ID','RECORD_USER','RECORD_TYPE','RECORD_TITLE','RECORD_NUMBER','DANGER_FACTOR','DEAL_OPINION','CONSTRUCT_RESPONSIBLE_PERSON_USER_ID','CONSTRUCT_RESPONSIBLE_PERSON_REALNAME','REVIEW_TIME','RECIFY_AND_REFORM_INFO','REVIEW_OPINION','EXTEND_INFO','CREATE_USER_NAME','CREATE_TIME','CREATE_IP','MODIFY_USER_NAME','MODIFY_TIME','MODIFY_IP','LOCAL_MODIFY_USER_NAME','LOCAL_MODIFY_TIME','LOCAL_MODIFY_STATE') VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    
    [self.db beginTransaction];
    
    for (SecurityPatrol *un in modelArr)
    {
        [self.db executeUpdate:insertSQLStatment withArgumentsInArray:@[DEFISNUL(un.id),DEFISNUL(un.proId),DEFISNUL(un.basicRecordId) ,DEFISNUL(un.recordUser),DEFISNUL(un.recordType),DEFISNUL(un.recordTitle),DEFISNUL(un.recordNumber),DEFISNUL(un.dangerFactor),DEFISNUL(un.dealOpinion),DEFISNUL(un.constructResponsiblePersonUserId),DEFISNUL(un.constructResponsiblePersonRealname),DEFISNUL(un.reviewTime),DEFISNUL(un.recifyAndReformInfo),DEFISNUL(un.reviewOpinion),DEFISNUL(un.extendInfo),DEFISNUL(un.createusername),DEFISNUL(un.createtime),DEFISNUL(un.createip),DEFISNUL(un.modifyusername),DEFISNUL(un.modifytime),DEFISNUL(un.modifyip),@"",@"",@""]];
    }
    
    [self.db commit];
    [self.db close];
}

// 安全记录-危险性较大工程
- (void)saveDangerousWork:(NSMutableArray *)dangerousWorkArray {
    NSMutableArray *modelArr=[NSMutableArray array];
    for (int i=0; i<dangerousWorkArray.count; i++) {
        DangerousWork *addressM=[DangerousWork yy_modelWithJSON:dangerousWorkArray[i]];
        [modelArr addObject:addressM];
    }
    
    [self.db open];
    [self.db setShouldCacheStatements:YES];
    
    static NSString *insertSQLStatment = @"INSERT OR REPLACE INTO SAFE_DANGER ('ID','PRO_ID','BASIC_RECORD_ID','RECORD_USER','RECORD_TYPE','RECORD_TITLE','RECORD_NUMBER','CHECK_NAME','CHECK_TYPE','CHECK_INFO','IS_ON_POSITION','EXTEND_INFO','CREATE_USER_NAME','CREATE_TIME','CREATE_IP','MODIFY_USER_NAME','MODIFY_TIME','MODIFY_IP','LOCAL_MODIFY_USER_NAME','LOCAL_MODIFY_TIME','LOCAL_MODIFY_STATE') VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    
    [self.db beginTransaction];
    
    for (DangerousWork *pro in modelArr)
    {
        [self.db executeUpdate:insertSQLStatment withArgumentsInArray:@[DEFISNUL(pro.id),DEFISNUL(pro.proId),DEFISNUL(pro.basicRecordId),DEFISNUL(pro.recordUser),DEFISNUL(pro.recordType),DEFISNUL(pro.recordTitle),DEFISNUL(pro.recordNumber),DEFISNUL(pro.checkName),DEFISNUL(pro.checkType),DEFISNUL(pro.checkInfo),DEFISNUL(pro.isOnPosition),DEFISNUL(pro.extendInfo),DEFISNUL(pro.createusername),DEFISNUL(pro.createtime),DEFISNUL(pro.createip),DEFISNUL(pro.modifyusername),DEFISNUL(pro.modifytime),DEFISNUL(pro.modifyip),@"",@"",@""]];
    }
    [self.db commit];
    [self.db close];
}

// 安全记录-施工设备进场
- (void)saveWorkDeviceApproach:(NSMutableArray *)workDeviceApproachArray{
    
    NSMutableArray *modelArr=[NSMutableArray array];
    for (int i=0; i<workDeviceApproachArray.count; i++) {
        WorkDeviceApproach *addressM=[WorkDeviceApproach yy_modelWithJSON:workDeviceApproachArray[i]];
        [modelArr addObject:addressM];
    }
    
    [self.db open];
    [self.db setShouldCacheStatements:YES];
    
    static NSString *insertSQLStatment = @"INSERT OR REPLACE INTO SAFE_CONSTRUCT_DEVICE ('ID','PRO_ID','BASIC_RECORD_ID','RECORD_USER','RECORD_TYPE','RECORD_TITLE','RECORD_NUMBER','DIVICE_NAME','WORK_TYPE','USER_ADDR','IS_MEET_QULIFICATION','IS_MEET_STANDARD','IN_REGISTER','IS_AGREE','EXTEND_INFO','CREATE_USER_NAME','CREATE_TIME','CREATE_IP','MODIFY_USER_NAME','MODIFY_TIME','MODIFY_IP','LOCAL_MODIFY_USER_NAME','LOCAL_MODIFY_TIME','LOCAL_MODIFY_STATE') VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    
    [self.db beginTransaction];
    
    for (WorkDeviceApproach *pro in modelArr)
    {
        [self.db executeUpdate:insertSQLStatment withArgumentsInArray:@[DEFISNUL(pro.id),DEFISNUL(pro.proId),DEFISNUL(pro.basicRecordId),DEFISNUL(pro.recordUser),DEFISNUL(pro.recordType),DEFISNUL(pro.recordTitle),DEFISNUL(pro.recordNumber),DEFISNUL(pro.diviceName),DEFISNUL(pro.workType),DEFISNUL(pro.userAddr),DEFISNUL(pro.isMeetQulification),DEFISNUL(pro.isMeetStandard),DEFISNUL(pro.inRegister),DEFISNUL(pro.isAgree),DEFISNUL(pro.extendInfo),DEFISNUL(pro.createusername),DEFISNUL(pro.createtime),DEFISNUL(pro.createip),DEFISNUL(pro.modifyusername),DEFISNUL(pro.modifytime),DEFISNUL(pro.modifyip),@"",@"",@""]];
    }
    
    [self.db commit];
    [self.db close];
}


@end

